spade78
spade78

Reputation: 1959

Haskell in GHCI: Why do I need parens to make this pattern match work?

So using GHCI, these statements are equivalent which makes sense to me because the list expression in end0 is syntactic sugar for the list expression in end1:

let end0 [x,y,z] = z

let end1 (x:y:z:[]) = z

But taking the parens out of the pattern of end1 gives me an "Parse error in pattern" error. So why is that? Do the parens have special meaning in a pattern match or is it a precedence issue like I normally think of when I use parens with operators?

Upvotes: 3

Views: 454

Answers (2)

Wei Hu
Wei Hu

Reputation: 2958

Because without the parens, it's parsed as let (end1 x):y:z:[] = z.

Upvotes: 6

rapfaria
rapfaria

Reputation: 2577

It has to do with precedence.

A function takes precedence over :, so GHC would infer that you are defining the function for x only. That's why you have to pack it all inside parens.

Upvotes: 7

Related Questions