m (caps - minor rewording)
(Fixed the Haskell tutorial link)
Latest revision as of 22:24, 21 December 2011
An anonymous function is a function without a name. It is a Lambda abstraction and might look like this:
 1 Examples
Prompt> (\x -> x + 1) 4 5 :: Integer
Or I could make a nameless function of two parameters, x and y:
Prompt> (\x y -> x + y) 3 5 8 :: Integer
You could also name a lambda expression if you wanted to for some reason:
addOne = \x -> x + 1
Of course, there are a variety of better ways to write that in Haskell, but you get the idea.
 2 But why bother?Sometimes it is more convenient to use a lambda expression rather than giving a function a name. This is often the case when using
addOneList lst = map addOne' lst where addOne' x = x + 1
But here's another way, where we pass the anonymous function into map rather than any named function.
addOneList' lst = map (\x -> x + 1) lst
addOneList'' = map (+1)