Erlang Language Data Types Lists

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

A list in Erlang is a sequence of zero or more Erlang terms, implemented as a singly linked list. Each element in the list can be any type of term (any data type).

1> [1,2,3].
[1,2,3]
2> [wow,1,{a,b}].     
[wow,1,{a,b}]

The list's head is the first element of the list.

The list's tail is the remainder of the list (without the head). It is also a list.
You can use hd/1 and tl/1 or match against [H|T] to get the head and tail of the list.

3> hd([1,2,3]).
1
4> tl([1,2,3]).
[2,3]
5> [H|T] = [1,2,3].
[1,2,3]
6> H.
1
7> T.
[2,3]

Prepending an element to a list

8> [new | [1,2,3]].
[new,1,2,3]

Concatenating lists

9> [concat,this] ++ [to,this].
[concat,this,to,this]

Strings

In Erlang, strings are not a separate data type: they're just lists of integers representing ASCII or Unicode code points:

> [97,98,99].
"abc"
> [97,98,99] =:= "abc".
true
> hd("ABC").
65

When the Erlang shell is going to print a list, it tries to guess whether you actually meant it to be a string. You can turn that behaviour off by calling shell:strings(false):

> [8].
"\b"
> shell:strings(false).
true
> [8].
[8]

In the above example, the integer 8 is interpreted as the ASCII control character for backspace, which the shell considers to be a "valid" character in a string.



Got any Erlang Language Question?