Skip to Content
Introducing Erlang, 2nd Edition
book

Introducing Erlang, 2nd Edition

by Simon St. Laurent
March 2017
Beginner
212 pages
4h 47m
English
O'Reilly Media, Inc.
Content preview from Introducing Erlang, 2nd Edition

Chapter 6. Lists

Erlang is great at handling lists, long series of 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

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 less varied ...

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

Erlang Programming

Erlang Programming

Francesco Cesarini, Simon Thompson
Erlang and OTP in Action

Erlang and OTP in Action

Eric Merritt, Richard Carlsson, Martin Logan
Introducing Erlang

Introducing Erlang

Simon St. Laurent

Publisher Resources

ISBN: 9781491973363Errata PageSupplemental Content