Given a function like negate, it has the type signature:
negate :: Num a => a -> a
which I would describe as a being the type in the context of Num (correct me if you think I am wrong).
But I not fully sure how to describe something like last, which has the type signature:
last :: [a] -> a
My guess would be to say it isn't type-specific, and that it takes a list and produces a single value of the same type as the list. Is this the correct way to think about it?
First, a is not the type in the context of Num, but a type that has a Num instance.
Num a => a -> a is a constrained polymorphic type, while [a] -> a is an unconstrained polymorphic type, or just polymorphic type for short. In the unconstrained case, a can be any type; in the constrained case, it must be a type that obeys the given constraints.
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