Data Types
Constants whose value is their name.
42, -7, 1_000_000 (OTP 23+), 16#FF, 2#1010, $a (=97) 3.14, 1.0e-10, 2.5e3
ok, error, 'Hello World', true, false, undefined {ok, 42}, {point, 1, 2} [1, 2, 3], [], [H | T] "hello" (a list of integers!) <<1, 2, 3>>, <<"hello">> #{name => "Ann", age => 30} fun(X) -> X * 2 end compile-time sugar over tuples
4.1 Numbers
1> 10 / 3. % float division
3.3333333333333335
2> 10 div 3. % integer division
3
3> 10 rem 3. % remainder
1
4> 2 * 1000000000000000000000. % arbitrary precision integers (bignums)
2000000000000000000000
5> math:pow(2, 10).
1024.0
6> round(3.6), trunc(3.6), float(3), abs(-5).
7> integer_to_list(255, 16).
"FF"
8> list_to_integer("42"), binary_to_integer(<<"42">>), float_to_list(1.5, [{decimals, 2}]).4.2 Atoms
Constants whose value is their name. Start with a lowercase letter or are quoted.
true and false are atoms; there's no separate boolean type. ⚠️ Atoms are not garbage collected; the default limit is 1,048,576. Never create atoms from untrusted input (list_to_atom/1) — use list_to_existing_atom/1 / binary_to_existing_atom/2.
ok, error, 'EXIT', 'with spaces', node@host4.3 Tuples
Convention: first element is an atom tag describing the tuple ({ok, Value}, {error, Reason}).
Point = {point, 10, 20},
element(2, Point), % 10 (1-based!)
setelement(2, Point, 99), % {point, 99, 20}
tuple_size(Point), % 3
{point, X, Y} = Point. % destructure4.4 Lists
Lists can mix types: [1, atom, "str", {a, b}].
L = [1, 2, 3],
[0 | L], % [0,1,2,3] — cons
[H | T] = L, % H = 1, T = [2,3]
L ++ [4], % [1,2,3,4]
[1,2,3,2] -- [2], % [1,3,2]
length(L), hd(L), tl(L).4.5 Strings are lists of integers
Binary strings <<"text">> are more memory-efficient (see §14–15).
1> "abc" =:= [97, 98, 99].
true
2> [72, 105].
"Hi"
3> io:format("~w~n", ["Hi"]).
[72,105]4.6 Type-check BIFs
is_integer/1, is_float/1, is_number/1, is_atom/1, is_boolean/1, is_list/1, is_tuple/1, is_map/1, is_binary/1, is_bitstring/1, is_function/1, is_function/2, is_pid/1, is_port/1, is_reference/1, is_record/2.
4.7 Conversion BIFs
atom_to_list, list_to_atom, atom_to_binary, binary_to_atom, integer_to_list, list_to_integer, float_to_list, list_to_float, tuple_to_list, list_to_tuple, binary_to_list, list_to_binary, term_to_binary, binary_to_term, integer_to_binary, binary_to_integer, float_to_binary, pid_to_list, iolist_to_binary, map:to_list.