For example, I can't do
f = ((*2).(+)) 3
which is alright since I can always do
f = (*2).(+3)
but this is slightly inconvenient when I want to create a list of functions, e.g.
map ((*2).(+)) [1, 2, 3]
whereas
map (+) [1, 2, 3]
would be alright.
Of course, I can always use lambda notation to implement currying:
map (\x y -> 2*(x + y)) [1, 2, 3]
As far as I can tell, GHC doesn't like composing partially applied functions because it doesn't know how to feed function types to operations like (*2).
(+) 2 :: Num a => a -> a
(*2) :: Num a => a -> a
But I've always thought it should be a rather natural thing: the type of the output of (+) is Num a => a, so (*2) should be able to 'eat' that.
My question is: is this implemented in some way? Or, does someone have any idea why such a straightforward thing isn't implemented in Haskell?
(f . g) xis equivalent to(f (g x))but you are hoping that((* 2) . (+)) nwill be the same as((* 2) . (+ n)), when clearly it is really(* 2) ((+) n)by the definition of(.), which is not the same. You need a different function than(.)for that sort of composition. - Alexis Kingf = ((*2).(+)) 3can be written asf = (.) (*2) . (+) $ 3. The pattern to do this is:(.) f1 . f2, wheref1andf2are functions that take 1 and 2 arguments respectively. - mschmidtmap ( (.) (*2) . (+) ) [1,2,3]which gives you a list of functions of typeNum a => a -> aeach. - Thilo