2. Funs
2 Funs
2.1 map
The following function, double
, doubles every element in a list:
double([H|T]) -> [2*H|double(T)]; double([]) -> [].
Hence, the argument entered as input is doubled as follows:
> double([1,2,3,4]). [2,4,6,8]
The following function, add_one
, adds one to every element in a list:
add_one([H|T]) -> [H+1|add_one(T)]; add_one([]) -> [].
The functions double
and add_one
have a similar structure. This can be used by writing a function map
that expresses this similarity:
map(F, [H|T]) -> [F(H)|map(F, T)]; map(F, []) -> [].
The functions double
and add_one
ca