I'm wondering where I can find information about the "a
" used in the Length example. It seems to be a type of some kind?

- 2,761
- 5
- 24
- 28

- 630
- 5
- 14
2 Answers
[1,2,3]
is a List Int
, functions that could only work with a Lists of Ints would have to have List Int
in their type signature. ["a", "b"]
is a List String
, functions that could only work with a Lists of Strings would have to have List String
in their type signature. A function that works with a list of any type (for example List.length
) can have a generic type signature (like List a
or List b
). The meaning of a
is only relevant within the type signature. For example a function with a type of List a -> a
, when given a List Int
would return an Int
. When given a List String
it would return a String
.
Take for example the map
function which has a signature of (a -> b) -> List a -> List b
. It says that given a function that takes an a
and returns a b
, and a List a
, it will return a List b
.
Given a function that takes a String
and returns an Int
, and a List String
, map
will return a List Int
.
List.map String.length ["a", "aa", "aaa"]
-- [1, 2, 3] : List Int
Given a function that takes an Int
and returns a String
, and a List Int
, map
will return a List String
.
List.map (\n -> String.repeat n "a") [1, 2, 3]
-- ["a", "aa", "aaa"] : List String

- 7,160
- 2
- 33
- 45
-
Thank you, I now understand. – doobdargent Jul 07 '15 at 14:49
a
is a type variable.
This means that the type List
is polymorphic (generic), i.e. a
can be substituted by any type (Int
, String
, ...). The function length
is polymorphic too. For example, length [1, 2, 3]
will return 3
, length ["word1", "word2"]
will return 2
.

- 36,558
- 20
- 126
- 155
-
I'm not sure I understand, I don't see the type polymorphism you are referring to. Considering your example, length [1,2,3] or length ["word1", "word2"], for each call it receives a List as input, and outputs an integer. – Mihai Cicu Jul 07 '15 at 12:44
-
But `a` is undefined right ? If I replace `a` with `b` or `c` or pretty much anything, it'll also work. It's confusing ! – doobdargent Jul 07 '15 at 12:56
-
@doobdargent There is a simple rule to distinguish type variables from types (`List Int` vs `List a`): type names must begin with an uppercase letter, type variable names must start with a lowercase letter. – ZhekaKozlov Jul 07 '15 at 13:12
-
@MihaiCicu It cannot receive `List`, because `List` is not a type (it is a type constructor actually, but this is a more complicated thing). I.e. you cannot declare a function of type `List -> Int` (it will not compile). – ZhekaKozlov Jul 07 '15 at 13:20
-
with robertjlooby's explanation, now I understand what u meant. thanks anyway – Mihai Cicu Jul 10 '15 at 11:40