Reputation: 1398
This question is about type in Haskell programming language.
When I type
:t []
I get the return as:
[] :: [a]
What is [a]
?
Upvotes: 0
Views: 220
Reputation: 20960
Rewriting it like so might make things clearer:
[] :: forall a. [a]
So, since it doesn't contain any value, haskell can't decide on a type and leaves it open. If you use it in a typed expression, however, like
x = []
f :: [Int] -> [Int]
f l = l
y = f x
it automatically gets resolved to [Int]
, since it is used as this type.
Upvotes: 12
Reputation: 185701
It's a placeholder. It basically means any type could go there. In other words, if you had a function
someFunc :: [Int] -> ()
You could pass []
to it because []
is [a]
, which will match [Int]
(by replacing the a
placeholder with the concrete Int
type).
Now if you look at, say, id
, you'll find it has the type a -> a
. This means that it's a function that takes an object of any type, and returns an object of the same type. Interestingly, because the function knows absolutely nothing about the object it's given (since it has no constraints on the type), you know that this function has to do either of two things:
Upvotes: 5