I'm just learning Haskell and functional programming using Richard Bird's book and came across the type signature of the (.) function. Namely
(.) :: (b -> c) -> (a -> b) -> (a -> c)
and the associated definition
(f . g) x = f (g x)
I understand what the operator is doing but am a bit confused about how to read the type signature. Is it saying that (.) takes as its first argument a function of type (b -> c), then returns a function of type (a -> b), and finally returns a function of type (a -> c)? Is this the right way to read the type signature?
Also, could this be an example of currying, where (.) is a curried function that takes two parameters? Or is that not the correct way to think about currying?
You've almost got it, it takes a b -> c
and returns a function (a -> b) -> (a -> c)
which, when given an (a -> b)
returns a function a -> c
. It may also be helpful to know that in Haskell, you can wrap an operator in parens and use it prefix so
f . g === (.) f g
Now it's easier to see the currying
((.) f) g === f . g
Finally, notice that this type signature is equivalent to
(b -> c) -> (a -> b) -> a -> c
Since ->
is right associative.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With