Reputation: 31
Suppose I have a function like map zipWith
, how can I determine its type? Given that the type of zipWith
is (a -> b -> c) -> [a] -> [b] -> [c]
and that of map
is (a -> b) -> [a] -> [b]
Similarly how do I determine types of functions like zipWith sum
?
Upvotes: 1
Views: 410
Reputation: 48766
You can see its type in ghci:
ghci> :t map zipWith
map zipWith :: [a -> b -> c] -> [[a] -> [b] -> [c]]
Similarly for zipWith sum
:
ghci> :t zipWith sum
zipWith sum :: Num (b -> c) => [[b -> c]] -> [b] -> [c]
Upvotes: 1
Reputation: 34398
You can check the type in GHCi with :t
, as mentioned in the other answers. If you want to try figuring it out yourself, you need to substitute the types as appropriate. In your first example, we have
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
and
map :: (a -> b) -> [a] -> [b]
The first argument of map
is a function of one argument, so we have to regard zipWith
as such a function:
zipWith :: (a -> b -> c) -> ([a] -> [b] -> [c])
(The type above is equivalent to the original one. It means zipWith
converts a function which takes arguments of types a
and b
to a function which takes lists of a
and b
.)
map
can be seen as a function of one argument as well:
map :: (a -> b) -> ([a] -> [b])
Now, we fill in the types in map
's result type - a
becomes a -> b -> c
and b
becomes [a] -> [b] -> [c]
:
map zipWith :: [a -> b -> c] -> [[a] -> [b] -> [c]]
P.S.: Do you really want functions which take lists of functions as arguments? If you just want to zip two lists adding the corresponding elements you want
zipWith (+) :: Num c => [c] -> [c] -> [c]
rather than zipWith sum
.
Upvotes: 5
Reputation: 1870
That's what ghci
is there for! Or you can just use tryhaskell.org
> :t map zipWith
:: [a -> b -> c] -> [[a] -> [b] -> [c]]
Upvotes: 0