开发者

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

开发者 https://www.devze.com 2023-01-30 14:47 出处:网络
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:

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 pa开发者_开发技巧rens with operators?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号