In GHCi, doing the following yield:
:t (==)
(==) :: Eq a => a -> a -> Bool
or
:t elem
elem :: (Eq a, Foldable t) => a -> t a -> Bool
I am confused with the arrow going from a to a, and then a to Bool. Is it because == or elem is a curried function?
The type signature for elem is very similar to ==, except for the additional t. What does ta
in :t elem
mean?
Yes, operators are curried. Let's ignore the contexts for a second (the part of the type before =>
).
(==) :: a -> a -> Bool
(->)
associates to the right, so this means:
(==) :: a -> (a -> Bool)
That is, it's a function which, given an a
, returns another function, which in turn takes an a
and gives a Bool
. It returns the function which is only true when its argument is equal to the first a
.
(We will set NoMonomorphismRestriction
because otherwise it will just be extra confusing for no good reason)
ghci> :set -XNoMonomorphismRestriction
ghci> let f = (==) 1
ghci> f 1
True
ghci> f 2
False
-- or just
ghci> (==) 1 2
False
The part before the ( =>
) puts constraints on types. Eq a
means that a
must be a type that supports equality.
As for the t
in elem
, that might be a bit advanced for you to understand fully right now. I'll give you a little bit. Because of the context
(Eq a, Foldable t) => ...
we know that t
has to be Foldable
. Lists are foldable, and foldable doesn't mean much more than "has a toList
method". So you can read elem
as:
elem :: (Eq a) => a -> [a] -> Bool
and in general, when you see a foldable type, just pretend it's a list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.