Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell complex function composition

I'm trying to learn function composition in Haskell. I have following exercise.
I have function:h x y = f (g x y)and i need to find function composition equal to it:

a) f . g  
b) (f.).g  
c) (.)f(.g)  
d) f(.g)

I know that (.)f g = f.g = f(g x) but i don't understand this complicated function composition

like image 704
Tomasz Avatar asked Sep 20 '26 23:09

Tomasz


1 Answers

The correct answer is (b): (f.).g

Let us analyze this function. The (f.) part is short for ((.) f), so we already solved that one, and thus the function - without syntactical sugar - is:

(.) ((.) f) g

Now we can rewrite the first (.) function to a lambda-expression:

\x -> ((.) f) (g x)

And now when we evaluate the second function on the left (((.) f)) further, we obtain:

\x -> (.) f (g x)

or:

\x -> f . g x

So if we convert the (.) function in a lambda expression, we obtain:

\x -> (\y -> f ((g x) y))

Now we can make this expression more elegantly. (g x) y can be rewritten to g x y:

\x -> (\y -> f (g x y))

and we can rewrite nested lambda expressions into a single lambda expression:

\x y -> f (g x y)

Which is what we wanted.

like image 84
Willem Van Onsem Avatar answered Sep 22 '26 18:09

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!