List.map
has the signature ('a -> 'b) -> 'a list -> 'b list
which in English is a function that takes a function (we'll call this the mapping function) from one type (namely 'a
) to another type (namely 'b
) and a list of the first type. The function returns a list of the second type where every element is the result of calling the mapping function on an element of the first list.
List.map string_of_int [ 1; 2; 3; 4 ]
#- [ "1"; "2"; "3"; "4" ] : string list
The types 'a
and 'b
don't have to be different. For example, we can map numbers to their squares just as easily.
let square x = x * x in
List.map square [ 1; 2; 3; 4 ]
#- [ 1; 4; 9; 16 ] : int list