Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell interpretation of lambda function

i have one Haskell function, which i don't understand but want to.

 i :: Int
 i = ((\g x -> g x + g x) (\y -> y)) 3

I know what a lambda function is: a nameless function. E.g. (\x -> x) 3 takes 3 and returns it, (\x y -> x+y) 3 4 takes 3, 4 and returns 7. But in this special case i can't interpret it. I hope you can help me. Btw. the solution for this function is 6.

like image 314
Stan Avatar asked Jun 24 '26 16:06

Stan


1 Answers

Now your (\y -> y) function is equivalent to id. Let's rewrite your function using that:

i = ((\g x -> g x + g x) id) 3

Now apply the id function to (\g x -> g x + g x). This will get reduced to:

i = (\x -> id x + id x) 3

Now it's simple:

i = id 3 + id 3
i = 6
like image 143
Sibi Avatar answered Jun 26 '26 09:06

Sibi