2

Typing the following into a GHC interpreter yields no error:

let head' (x:_) = x

But if we remove the parentheses:

let head' x:_ = x

...we obtain:

Parse error in pattern: head'

Why are the parentheses necessary?

George
  • 6,927
  • 4
  • 34
  • 67
  • 4
    because the other patter would be equal to `(head' x) : _` (remember application has the highest precedence) and this would only make sense for an Data-Constructor – Random Dev Mar 29 '16 at 15:40
  • Possible duplicate of [Haskell: Parse error in pattern](https://stackoverflow.com/questions/8561762/haskell-parse-error-in-pattern) – Chris Martin Sep 06 '17 at 08:04

1 Answers1

10

In Haskell, function application has higher precedence than any operator, and pattern-matching reflects that.

So, without the parentheses, head' x:_ is parsed as (head' x):_, which doesn't make sense in this context, and causes an error.

comingstorm
  • 25,557
  • 3
  • 43
  • 67