cult3

Understanding the Types in Elixir

Mar 14, 2016

Table of contents:

  1. Variables
  2. Atoms
  3. Booleans and Nil
  4. Numbers
  5. Lists
  6. Tuples
  7. Keyword Lists
  8. Regular Expressions
  9. Conclusion

If you are looking to start building Erlang and Elixir applications I’m going to assume this is not your first time at the rodeo.

When you are looking to start building your first web application, you are probably better off learning a programming language such as PHP, Ruby, or Python due to the vast number of learning resources, tutorials, guides, and videos that will explain just about every concept you would need from the perspective of someone who has never wrote code before.

Instead of explaining these fundamental programming concepts, I’m going to skip over the newbie programmer stuff, and jump straight into what’s new or interesting to an Object-Oriented Programmer when discovering the world of Erlang and Elixir.

In todays tutorial we’re going to be looking at some of the basic building blocks of Elixir.

As we saw in Getting started with Elixir, in order to quickly get started writing Elixir code, we can use Interactive Elixir. Run the following command in Terminal to start Interactive Elixir:

iex

For the rest of this tutorial you will be able to run the code in iex.

Variables

One of the essential building blocks of most programming languages are variables:

my_age = 27

Here I’ve bound the value 27 to the variable my_age.

Elixir is a dynamic language and so we don’t have to tell the variable what type it should be.

Variables in Elixir should always start with a lowercase alphabetic character, or an underscore and should only contain alphanumeric and underscore characters.

An important thing to note about data in Elixir is that it is immutable:

my_age + 1

If you run the code above iex will return 28, but if you take another look at the my_age variable, it will still be 27.

However, you can rebind the variable to a new value:

my_age = 23

When you rebind a variable in Elixir, it does not mutate the existing memory location, but rather, it reassigns the name to a new location.

In Elixir, the data is immutable, but variables can be rebound and therefore variables are mutable.

Atoms

If you are familiar with Symbols in Ruby, Atoms are basically the same thing:

:hello

An Atom is a word that starts with a :. You can also include underscores or you can have multi-word Atoms by wrapping the words in speech marks:

:hello_world
:"hello world"

The name of an Atom is also it’s value. Therefore, two Atoms of the same name will evaluate as true:

# true
:hello == :hello
# false
:hello == :world

Booleans and Nil

Booleans and Nil are just Atoms of the given value:

# true
true
# false
false
# nil
nil

For your convenience you can also just drop the :. For example the following values are equal:

true == true
false == false
nil == nil

Everything except :false and :nil is considered a truthy value.

Numbers

There is nothing really interesting to say about Numbers in Elixir. If you have done even the most basic introduction to programming you will know everything you need to know about Numbers.

1 + 1
# 2
5 - 2
# 3
4 * 5
# 20

The one interesting thing to note is that when you do division, you will always be returned a float (a value with a decimal place)

100 / 2
# 50.0

If you want to do integer division, you must use the div function:

div(100, 2)
# 50

Lists

Lists are a bit like Arrays that you might be familiar with from other languages:

[1, 2, 3]
[:hello, :world]
["a", [1, 2, 3], :whatup]

Lists are implemented as Linked Lists and so a common thing you will see them used for is getting the head or the tail.

The head of the list is the first item, and the tail is all of the remaining items of the list:

list = [:one, :two, :three]

hd(list)
# :one

tl(list)
# [:two, :three]

Tuples

Tuples are a “list” type (a bit like an Array) that can contain items of different values:

{:hello, :world}

{1, :two, "three"}

Tuples are used for storing related bits of data. We’ll cover Tuples in more depth in a future tutorial.

Keyword Lists

A Keyword List is basically the same as a Hash that you may have you seen in other languages. A Keyword List is just a collection of key value pairs:

[first_name: "Philip", last_name: "Brown"]

Under the hood, Elixir is just converting this to a List of two element Tuples.

Regular Expressions

Regular Expressions are pretty much what you would expect if you are already familiar with Regular Expressions:

Regex.match?(~r/foo/, "foo")

Here I’m simply checking for a match in the given string.

Conclusion

In today’s tutorial we’ve taken a whirlwind tour of some of the basic building blocks of the Elixir programming language. If you are already familiar with a programming language like PHP, Python, or Ruby, this should of been very gentle introduction.

The beautiful thing about learning a new programming language is the fact that many of the concepts you are already familiar with are also relevant in the new language. It’s much harder to go from 0 to 1 programming languages than it is to go from 1 to N.

But there are some important differences between Elixir and languages such as PHP, Python, and Ruby, and so we will continue to explore those difference next week.

Philip Brown

@philipbrown

© Yellow Flag Ltd 2024.