## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# 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 ...

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required