I've defined a function f1 and f2,so that I can use at the and the function composition (fkomp
), which is supposed to use f1 and f2 to calculate 2^x
by every element in a given List.
f1 :: Int -> Int
f1 x = product (replicate x 2)
f2 :: (a -> b) -> [a] -> [b]
f2 f xs = [f x | x <- xs]
fkomp :: [Int] -> [Int]
fkomp xs = f2 f1 $ xs
It works,but the problem is,that i can't write my code with composition:
fkomp xs = f2.f1 $ xs
I've been typing every single combination but it doesn't work with composition.
Could someone lighten my path ?
Thanks a lot
Ok, let's look just at the types (it's like a puzzle - the types have to fit):
in order to compose the both you need ones co-domain to be the same as the others domain.
This is because the composition has type:
See the
b
has to fit ;)So for your
f1
andf2
you would need eitherInt ~ (a -> b)
orInt ~ ([a] -> [b])
both of which are not working well (as you found out).BUT you kind of have the ability to apply
f1
tof2
asf1
just fitsf2
first argument (as you have seen too) - so I am a bit confused why you even want to use composition here.remarks
your functions are a bit strange - I think the usual way to write them would be
or even
note that the last one is not function-composition but (just as your case) function-application: I apply the function
(2^) :: Int -> Int
tomap :: (Int -> Int) -> [Int] -> [Int]
and get a function of type[Int] -> [Int]
as the result (if you check the types in GHCi you will see a more generic versions but I think this is a bit more clear)