Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 552
spade78 Avatar asked Nov 26 '25 07:11

spade78


2 Answers

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.

like image 82
rapfaria Avatar answered Nov 28 '25 00:11

rapfaria


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

like image 35
Wei Hu Avatar answered Nov 28 '25 02:11

Wei Hu