Skip to Content
Introducing Erlang
book

Introducing Erlang

by Simon St. Laurent
January 2013
Beginner to intermediate content levelBeginner to intermediate
204 pages
4h 26m
English
O'Reilly Media, Inc.
Content preview from Introducing Erlang

Chapter 6. Lists

Erlang is great at handling lists, long series of similar (or not) values. List processing makes it easy to see the value of recursion, and offers opportunities to get a lot of work done for very little effort.

List Basics

An Erlang list is an ordered set of elements. Generally you will process a list in order, from the first item (the head) to the last item, though there are times when you may want to grab a particular item from the list. Erlang also provides built-in functions for manipulating lists when you don’t want to go through the entire sequence.

Erlang syntax encloses lists in square brackets and separates elements with commas. A list of numbers might look like the following:

[1,2,4,8,16,32]

The elements can be of any type, including numbers, atoms, tuples, strings, and other lists. When you’re starting out, it’s definitely easiest to work with lists that contain only a single type of element, rather than mixing all the possibilities, but Erlang itself has no such constraint. There is also no limit on the number of items a list can contain, though eventually you may find practical limits of memory.

You can pattern match with lists just as you can with other Erlang data structures:

1> [1,X,4,Y] = [1,2,4,8].
[1,2,4,8]
2> X.
2
3> Y.
8

Note

While it’s possible to use lists instead of tuples, your code will make more sense if you use tuples to handle data structures containing various kinds of data in a known structure, and lists to handle data structures containing ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Building Web Applications with Erlang

Building Web Applications with Erlang

Zachary Kessin
Études for Erlang

Études for Erlang

J. David Eisenberg
Erlang by Example with Cesarini and Thompson

Erlang by Example with Cesarini and Thompson

Simon Thompson, Francesco Cesarini
Erlang and OTP in Action

Erlang and OTP in Action

Eric Merritt, Richard Carlsson, Martin Logan

Publisher Resources

ISBN: 9781449331757Supplemental ContentErrata