Several built-in functions are available for conversion between lists and other data types. The principal BIFs are as follows:
atom_to_list(A)
Converts the atom A
to a list of ASCII character codes.
Example: atom_to_list(hello) =⇒ [104,101,108,108,111]
.
float_to_list(F)
Converts the floating point number F to a list of ASCII characters.
Example:
float_to_list(1.5) =⇒ [49,46,53,48,48,...,48]`.
integer_to_list(I)
Converts the integer I
to a list of ASCII characters.
Example: integer_to_list(1245) =⇒ [49,50,52,53]
.
list_to_atom(L)
Converts the list of ASCII characters in L
to an atom.
Example: list_to_atom([119,111,114,108,100]) =⇒ world`.
list_to_float(L)
Converts the list of ASCII characters in L
to a floating point number.
Example: list_to_float([51,46,49,52,49,53,57]) =⇒ 3.14159
.
list_to_integer(L)
Converts the list of ASCII characters in L
to an integer.
Example: list_to_integer([49,50,51,52]) =⇒ 1234
.
hd(L)
Returns the first element in the list L
.
Example: hd([a,b,c,d]) =⇒ a
.
tl(L)
Returns the tail of the list L
Example: tl([a,b,c,d]) =⇒ [b,c,d]
.
length(L)
Returns the length of the list L
Example: length([a,b,c,d]) =⇒ 4
.
There are also tuple_to_list/1
and list_to_tuple/1
, which are dealt with in next chapters. Several other list processing BIFs are also provided, for example, list_to_pid(AsciiList)
, pid_to_list(Pid)
. These are described in Appendix B.