Anonymous Functions
Anonymous functions (lambda functions) are expressions that represent a function without name.
\x -> x + 5 -- defines anonymous function that, given a x, returns x + 5
(\x -> x + 5) 3 -- applies the anonymous function over 3
8
Function with name:
double x = 2 * x -- equals to double = \x -> 2 * x
double 3
6
map double [1, 2, 3]
[2, 4, 6]
map (\x -> 2 * x) [1, 2, 3]
[2, 4, 6]
Anonymous functions are usually used when they are short and only used once. They are also useful for performing program transformations.
Multiple parameters:
\x y -> x + y
Equals to:
\x -> \y -> x + y
Which means
\x -> (\y -> x + y)