I’m quite a noob so sorry for this silly question. I can’t get why backslash before function in this statement is needed.
map (\n -> n + 1) [1, 2, 3, 4, 5]
Why is this not sufficient?
map (n -> n + 1) [1, 2, 3, 4, 5]
Thank you.
I’m quite a noob so sorry for this silly question. I can’t get why backslash before function in this statement is needed.
map (\n -> n + 1) [1, 2, 3, 4, 5]
Why is this not sufficient?
map (n -> n + 1) [1, 2, 3, 4, 5]
Thank you.
One reason might be for parsing efficiency.
In (n -> n + 1)
, it’s not clear it’s a function until ->
, at which point the parser needs to back-track to the open parentheses and re-parse it as arguments to a lambda expression and a lambda body, rather than as a “normal” expression.
In (\n -> n +)
, it’s clear from the \
that the following should be parsed as arguments to a lambda, so no back-tracking is required, which can be expensive.
Also, I think it’s necessary for delimiting where the arguments to the lambda start, as \a b c -> a + b + c
is a legal lambda expression.
The parser has to lookahead with \ anyway because \ is also a valid operator. I think the answer is simply because PS is a Haskell derivative and Haskell uses \
as an ASCII lambda symbol.