我有以下数据
data A = C1 String | A :@: A
deriving(Show)
app inp = case inp of
a1 :@: a2 -> (C1 "a") :@: (C1 "b")
_ -> C1 "c"
为什么案例会返回输入而不是(C1 "a") :@: (C1 "b")
?
*Test> app (C1 "c") :@: (C1 "d")
C1 "c" :@: C1 "d"
如果我将A :@: A
更改为C2 A A
答案 0 :(得分:8)
函数应用程序的优先级高于:@:
(或任何其他中缀运算符),因此app (C1 "c") :@: (C1 "d")
与(app (C1 "c")) :@: (C1 "d")
相同,而不是app ((C1 "c") :@: (C1 "d"))
。后者做你期望的:
*Main> app ((C1 "c") :@@: (C1 "d"))
C1 "a" :@@: C1 "b"
更为惯用的写作方式是app $ (C1 "c") :@: (C1 "d")
。