function - Haskell - lambda expression -
i trying understand what's useful , how use lambda expression in haskell. don't understand advantage of using lambda expression on convention way of defining functions. example, following:
let add x y = x+y
and can call
add 5 6
and result of 11 know can following:
let add = \x->(\y-> x+y)
and same result. mentioned before, don't understand purpose of using lambda expression. also, typed following code (a nameless function?) prelude , gave me error message.
let \x -> (\y->x+y) parse error (possibly incorrect indentation or mismatched backets)
thank in advance!
many haskell functions "higher-order functions", i.e., expect other functions parameters. often, functions want pass such higher-order function used once in program, @ particular point. it's more convenient use lambda expression define new local function purpose.
here's example filters numbers greater ten given list:
ghci> filter (\ x -> x && x > 10) [1..20] [12,14,16,18,20]
here's example traverses list , every element x
computes term x^2 + x
:
ghci> map (\ x -> x^2 + x) [1..10] [2,6,12,20,30,42,56,72,90,110]
Comments
Post a Comment