Chapter 3. Atoms, Tuples, and Pattern Matching

Erlang programs are at heart a set of message requests and tools for processing them. Erlang provides tools that simplify the efficient handling of those messages, letting you create code that is readable (to programmers at least) while still running efficiently when you need speed.

Atoms

Atoms are a key structure in Erlang. Technically they’re just another type of data, but it’s hard to overstate their impact on Erlang programming style.

Usually, atoms are bits of text that start with a lowercase letter, like ok or earth. They can also contain (though not start with) underscores (_) and at symbols (@), like this_is_a_short_sentence or me@home. If you want more freedom to start with uppercase letters or use spaces, you can put them in single quotes, like 'Today is a good day'. Generally, the one word lowercase form is easier to read.

Atoms have a value—it’s the same as their text. (Remember, the period after hello isn’t part of the atom—it ends the expression.)

1> hello.
hello

That’s not very exciting in itself. What makes atoms exciting is the way that they can combine with other types and Erlang’s pattern matching techniques to build simple but powerful logical structures.

Pattern Matching with Atoms

Erlang used pattern matching to make the examples in Chapter 2 work, but it was very simple. The name of the function was the one key piece that varied, and as long as you provided a numeric argument Erlang knew what you meant. Erlang’s pattern ...

Get Introducing Erlang 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.