Haskell中的奇怪案例陈述

时间:2010-10-22 05:29:03

标签: haskell syntax

我有以下数据

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

,则可以正常工作

1 个答案:

答案 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")