This is pattern matching. Elixir will bind a variable to perform pattern
matching, if possible, and so it can be used a lot like assignment. But it's not
the same thing. We'll look at that again momentarily.

Tuples

Tuples are a fixed-size collection of terms. You can create a tuple with curly
brackets and commas:

iex(18)> {:like, "this"}
{:like, "this"}

Here we've created a 2-tuple.

Tuples are implemented with contiguous areas of memory, so accessing them is
fast. You can use the elem function for that:

We'll talk more about that tomorrow, but I wanted to call this out. These
2-tuples are used to allow pattern matching for success and failure trivially.

Collections

There are a few collection types we care about: Lists, Keyword Lists, and Maps.

Lists

Lists are implemented as linked
lists. This means that the time it
takes to traverse the list is proportional to the length of the list. It also
means that you can't tell how long a list is without traversing the whole thing.

You can build a list in place:

iex(25)> [1, 2, 3]
[1, 2, 3]

You can also append to a list:

iex(26)> [1 | [2, 3]]
[1, 2, 3]

A single-element list is the same as an item appended to the empty list:

iex(27)> [1 | []] == [1]
true

If you're just prepending elements to a list, it's very fast. This is because it
just consists of a new list where the tail points to the existing list in
memory - there is no need to copy any data.

The first item in a list is known as its head, and the rest is known as its
tail. You can get these from a list with the hd and tl functions:

iex(28)> hd([1, 2, 3])
1
iex(29)> tl([1, 2, 3])
[2, 3]

Something to be aware of with lists is that they are used to store character
lists, or charlists. These are a common 'string'y type used in Erlang, but
less frequently used in Elixir. They are produced by surrounding characters in
single quotes:

iex(32)> 'abc'
'abc'

One important thing to note about this is that these are represented by lists of
integers that correspond to their ASCII codes.
Consequently, lists that only contain integers in the range of printable ASCII
codes are a bit confusing the first time you see them printed out:

iex(33)> [97, 98, 99]
'abc'
iex(34)> [97, 98, 99] == 'abc'
true

Keyword Lists

When specifying a small set of options, it's common to use Keyword Lists. These
are lists of 2-tuples where the first element is an atom.

Anonymous functions

To call an anonymous function, you must use a dot (.) between the function and
its arguments.

iex(46)> add_two.(3)
5

And that's it for our survey of the basics of Elixir's syntax. We'll cover more
as we run across it, but this should get you started pretty well.

Scripting

Now let's look at basic scripting in Elixir.

Making a script

To make a script, create a file that ends in .exs:

vim foo.exs

add_two=fnx->x+2end# Here we'll use `IO.puts`, which just outputs to the consoleIO.putsadd_two.(3)

Now you can run the script, with:

elixir foo.exs
# => 5

That's just a basic rehashing of what we've already done, though, albeit with a
script to hold our code.

Summary

In today's episode, we covered most of the basic syntax of Elixir. Tomorrow,
we'll look at the mix tool, creating modules, defining functions in modules,
pattern matching, and using the pipe operator. See you soon!

sign up for full access

Meet your expert

I've been building web-based software for businesses for over 18 years. In the last four years I realized that functional programming was in fact amazing, and have been pretty eager since then to help people build software better.