Search code examples
haskellsyntaxlet

Why do we need to put let in front of a function in a list


This is the original function

[let square x = x * x in (square 5, square 3, square 2)]

I tried to remove let from the line above, but it doesn't work.

[square x = x * x in (square 5, square 3, square 2)]

<interactive>:21:11: error:
    parse error on input ‘=’
    Perhaps you need a 'let' in a 'do' block?
    e.g. 'let x = 5' instead of 'x = 5'

Why let is necessary to be there?


Solution

  • List is created from expressions. Expression for introduction local variable name should start with let (or, more specifically, should use let ... in syntax). square x = x * x is a top-level function declaration.