This is an exercise:
-- Ex 12: recall the binary function composition operation
-- (f . g) x = f (g x). In this exercise, your task is to define a function
-- that takes any number of functions given as a list and composes them in the
-- same order than they appear in the list.
--
-- Examples:
-- multiCompose [] "foo" ==> "foo"
-- multiCompose [] 1 ==> 1
-- multiCompose [(++"bar")] "foo" ==> "foobar"
-- multiCompose [reverse, tail, (++"bar")] "foo" ==> "raboo"
-- multiCompose [(3*), (2^), (+1)] 0 ==> 6
-- multiCompose [(+1), (2^), (3*)] 0 ==> 2
As beginner, I am not able to solve that. I tried many approach, this one does not work :
multiCompose [] = (1*) $
multiCompose fs = (multiCompose (init fs)) (last fs) $
Following my current understanding, it should work since it can be developed as follow :
multicompose [(+1), (2^), (3*)] = multiCompose [(+1), (2^)] (3*) $
= multiCompose [(+1)] (2^) $ (3*) $
= multiCompose [] (+1) $ (2^) $ (3*) $
= (1*) $ (+1) $ (2^) $ (3*) $
My questions
- Could you help me with a valid answer to this exercise ?
- Could you help me to understand why my solution does not work ?
Thank you so much