Tuples
Suppose we want to group a fixed number of items into a single
entity. For this we’d use a tuple. We can
create a tuple by enclosing the values we want to represent in
curly brackets and separating them with commas. So, for example,
if we want to represent someone’s name and height, we might use
{joe, 1.82}
. This is a tuple containing an atom and a
floating-point number.
Tuples are similar to structs in C, with the difference that they
are anonymous. In C, a variable P
of type point
might be declared as follows:
| struct point { |
| int x; |
| int y; |
| } P; |
We’d access the fields in a C struct using the dot operator. So,
to set the x
and y
values in the point, we
might say this:
| P.x = 10; P.y = 45; |
Erlang has no type declarations, so to ...
Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.