Could you explain to me how the following expression works:
str a b = ((.).(.)) (0 -) (+) 1 2
I have checked it and GHCi said that it is -3
but I don't understand why.
I checked as well the following:
*Main> :t ((.).(.))
((.).(.)) :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c
but it doesn't helped me.
Any idea?
The operator
(.).(.)
is sometimes given an alias:You can view it as
(.)
, only it works when the second function takes two arguments. So this works, for example:This will take two numbers, sum them and then take the square root of the sum. The
.:
alias sort of makes visual sense, as you can imagine the colon representing the function of two arguments being connected to the function of one argument.If you want to, you can view the type signature of
(.)
like this:This means that
(.)
takes two functionsa -> b
andb -> c
and "pipes them together" and returns a function froma
directly toc
. The operator(.:)
works similarly – you can write its type signature asWhat it does is that it takes one function
a1 -> a2 -> b
and one functionb -> c
and it gives you back a function that goes directly froma1
anda2
toc
.If you want to verify this type for yourself, you can work your way to
(.:)
with the signature for(.)
. I will not do this for you, in part because it might end up being a wall of text and in part because it's a great exercise for you in reasoning about your code! If you need a place to start, remember thatIn the expression
which can also be written as
the two argument functions to
(.)
(a -> b
andb -> c
) are both(.)
themselves – so you get to replace a lot ofa
s andb
s andc
s with what they really represent!