GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing wiki.haskell.org — Anonymous function - HaskellWiki
ExamplesThat is a nameless function which increments its parameter, x. So in Hugs or GHCi, I might say: 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. 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 For completeness it's worth mentioning that this could be better written using a section, in pointfree style: addOneList'' = map (+1) See also
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |