tags:

views:

87

answers:

1

I have the following data

data A = C1 String | A :@: A
         deriving(Show)  

app inp = case inp of
     a1 :@: a2 -> (C1 "a") :@: (C1 "b")
     _         -> C1 "c"

Why does the case return the input instead of (C1 "a") :@: (C1 "b")?

*Test> app (C1 "c") :@: (C1 "d")
C1 "c" :@: C1 "d"

Works fine if i'll change A :@: A to C2 A A

+7  A: 

Function application has higher precedence than :@: (or any other infix operator), so app (C1 "c") :@: (C1 "d") is the same as (app (C1 "c")) :@: (C1 "d"), not app ((C1 "c") :@: (C1 "d")). The latter does what you expect:

*Main> app ((C1 "c") :@@: (C1 "d"))
C1 "a" :@@: C1 "b"

A more idiomatic way of writing this is app $ (C1 "c") :@: (C1 "d").

Travis Brown