Search code examples
haskelllambdaidentifier

Meaning of \ in Haskell identifier


This one of these tricks that when you're unfamiliar with a language is more difficult to find but everybody else knows about and use it.

In my case I wonder what does it mean when you have the name of a variable, say ts, and you put the symbol \ before it:

newtype Parser a = Parser (String -> [(String, a)])
produce :: a -> Parser a
produce x = Parser (\ts -> [(ts, x)])

My guess is this is abstracting the variable? If so what would be its translation to other languages like Scala?


Solution

  • \parameter1 ... parameterN -> expression is Haskell's syntax for lambdas. The Scala equivalent would be (parameter1, ..., parameterN) => expression (or, if we want to maintain the fact that the function is curried, parameter1 => ... => parameterN => expression).

    The backslash was chosen for this syntax since it's the ASCII character that looks most like a λ.