2

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.

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

3

(\g x -> g x + g x) takes the arguments g, which must be a function, and x, which must be a valid argument to that function, and then adds the result of applying g to x to itself.

In this case \y -> y is supplied as the value for g and 3 as the value for x, so we get (\y -> y) 3 + (\y -> y) 3. (y -> y) 3 is 3, so we get 3 + 3, which is 6.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.