I might be asking something very stupid and the answer might be very simple as "the implementor chose it that way", but here I go.
To add two numbers we can use any style: (10+) 4
or 10 + 4
or (10 + 4)
.
while if we have two functions, say add10
and multiply5
and compose them to make one function out of it, say add10andMultiply5
then
add10.mul5 10
seems to give an error while
add10.mul5 $ 5
will work and
(add10.mul5) 5
will work and
add10andMultiply5 5
will also work.
Any comments why the first one should not work ? Please enlighten me. Thanks.
Functional composition in Haskell has lower precedence than function application. So
add10.mul5 10
is parsed asadd10 . (mul5 10)
. Now, the type of.
is given as:The first argument (
add10
) has typeInt -> Int
, so we can establish that b isInt
, and hence.
expects its second argument to be of typea -> Int
. But instead it's of typeInt
. Adding the$
changes the association, and means that instead the function composition is done before applying the composed function to 10.Everything works in your first example because you're not composing functions, you're just applying them. The equivalent in this case would be trying to do
(10+) . 4
, which you will observe also fails.