\x -> x + 1
Tour of the Haskell Syntax
http://www.cs.uu.nl/~afie/haskell/tourofsyntax.html
の
Anonymous function can be made using lambda expressions. For example
\x -> x + 1
is a function with one parameter which it will add one to. Lambda expressions prove useful as arguments to for example map:
map (\xs -> zip xs [1..]) list
をやってみる。
まずは\x->x+1。
Prelude List> \x -> x + 1 Top level: No instance for (Show (a -> a)) arising from use of `print' at Top level Probable fix: add an instance declaration for (Show (a -> a)) In a 'do' expression: print it
Showクラスのインスタンスではありません、と・・・
じゃあ、ということで\x -> x + 1 1を実行。
Prelude List> \x -> x + 1 1 Top level: No instance for (Show (a -> a)) arising from use of `print' at Top level Probable fix: add an instance declaration for (Show (a -> a)) In a 'do' expression: print it:1:10: No instance for (Num (t -> a)) arising from the literal `1' at :1:10 Probable fix: add an instance declaration for (Num (t -> a)) In the second argument of `(+)', namely `1 1' In a lambda abstraction: \ x -> x + (1 1) In the definition of `it': it = \ x -> x + (1 1)
+が1 1に結合してる?
じゃあ、(\x -> x + 1) 1で!
Prelude List> (\x -> x + 1) 1 2
できた〜