Erlang 1: Starting Out

eric | May 1, 2019, 3:59 p.m.

Comments to the book "Learn You Some Erlang For Great Good" by Fred Hébert, Chapter 1 - Starting Out. Challenges: Remembering relevant shell commands and understanding immutability. Important learning goals: Getting to know the shell and understanding the basic syntax.

Learn You Some Erlang For Great Good by Fred HébertThis article series has come about as a result of my efforts to learn Erlang. In order to learn the language, I am reading the book "Learn You Some Erlang For Great Good" by Fred Hébert. Every time I come across something that I find difficult to understand I will make an effort to understand it and explain my understanding in detail. I will also - as far as possible - site alternative sources, possible exercises and more. First off, maybe you could be a pal and buy Fred Herbert's book. There is a huge effort behind that book and I think Fred deserve our support. You can get it at No Starch Press.

I must admit that I am cheating a bit, because I read ahead quite a few chapters before rereading an earlier chapter and then commenting on that chapter on this site. This is to discover what is important to remember and / or easy to forget as I dig deeper into the content.

Each article has a list of references that I stongly suggest you have a look at, including the Erlang Language Reference, the ETS reference and Joe Armostrong's book Programming Erlang.

The Shell

If you are not used to Emacs, the shell might take some time getting used to, but it's no big deal. The most important commands are:

  • erl - starts the shell from the command line.
  • help(). - displays all the available commands in the shell - remember the punctuation mark!
    • Note: h(). is command history - the list of previous commands in the shell.

 

help-command in the Erlang shell

help-command in the Erlang shell

 

  • Ctrl-G - Interrupts the shell.
  • h - displays all the available commands while interrupted.
  • s - starts a new shell
  • j - lists all jobs
  • c - change to the shell. Note: Pressing return twice after c 1 gets you to the shell in job 1.

 

The Erlang shell in interrupted state with Ctrl-G

The Erlang shell in interrupted state with Ctrl-G

 

Starting Out for Real

This section is fairly straight-forward as it is mostly about syntax. The points that I find hardest to get - or to remember - are some of the things that sets Erlang syntax apart from all languages I have learned previously.

Variables

  • Invariable variables: What is important to keep in mind is that the = operator in Erlang is a relational operator that tests for equality. It compares what is to the left of the operator with what is on the right of it. It will only work as an assignment operator if what is to the left of the operator is unbound (has no value assigned to it).
  • Variables must all begin with a capital letter. Anything else is either an atom or a reserved word. Underscore is an exception to that rule. It is a variable that stores nothing.

Atoms

  • Atoms are just atoms. They are simply literals that have their own name as a value. They should never be generated dynamically as they take up space.
  • Reserved words that can not be used as atoms are: after and andalso band begin bnot bor bsl bsr bxor case catch cond div end fun if let not of or orelse query receive rem try when xor

Boolean Expressions

This is a bit of a challenge. Coming from other - especially non-functional languages - and being told that

(1) 0 == false evaluates to false, and

(2) 1 < false evaluates to true

is a bit annoying. Things to keep in mind:

  • You can compare anything.
  • In terms of values, you have a type hierarchy: number < atom < reference < fun < port < pid < tuple < list < bit string
  • true and false have no meaning, they are just atoms.

So in example (1) above, we are testing the equality between the number 0 with the atom false, which is quite clearly false. In example (2), we are testing whether number 1 is less than the atom false. As atoms are higher up the hierarchy of values than numbers, this turns out to be true.

Tuples

A tuple is a finite list of elements. It is used to order elements when you know beforehand how many elements there are. Elements can be of any type. You will se a lot of tuples in Erlang. They are used all over the place, in particular in pattern matching expressions.The underscore wildcard is used when you do not care about the value that is being replaced by the wildcard.

 

Lists

What got me a bit confused the first time I tried to use a list is the fact that Erlang will print numbers as letters, unless one of the numbers cannot be represented as a letter. Fortunately, it is possible to print numbers as numbers with io:format (more on that in a bit). Another thing to remember is that a list created with the cons operator ( | ), for instance  [ 1 | 2] , gives us a so-called "improper list" that cannot be used with standard list functions.

List Comprehensions

First off, the Erlang list comprehensions might seem complicated. They aren't.

[3+N | N <- [1,2,3,4]]

just means "make a list from the list 1,2,3,4 and add 3 to each element in the list".

in <-

constructor |

Adding a filter that sorts through the list we are using to construct a new list is also easy:

[3+N | N <- [1,2,3,4], N rem 2 =:=0]

which means "make a list from elements in the list 1,2,3,4 that are dividable by 2, and add 3 to each element in the list".

References

  • [1] Learn You Some Erlang - Starting Out - http://learnyousomeerlang.com/starting-out
  • [2] Erlang Reference Manual - Data Types - http://erlang.org/doc/reference_manual/data_types.html

Other Erlang Resources

Sites

The Erlang main site

Erlang Reference Manual User's Guide

Erldocs - An alternative to the official sites.

Erlang Patterns - A collection of Erlang patterns

Rebar3 - A build tool for Erlang that makes it easy to compile and test Erlang applications and releases.

Communities

Erlang mailing lists and forums

The Google group Erlang Programming

Erlang on Stack Exchange

Erlang on Freenode - Use #Erlang

Erlang on Slack

Books

"Learn You Some Erlang For Great Good", by Fred Hebert

"Programming Erlang", by Joe Armstrong

Articles

The Zen of Erlang, by Fred Hebert. A partly practical, partly philosophical take on Erlang.

Other

Ericsson's coding standard for Erlang - Programming rules and conventions.

Getting started with Erlang using IntelliJ IDEA (including Rebar3).

About Me

Experienced dev and PM. Data science, DataOps, Python and R. DevOps, Linux, clean code and agile. 10+ years working remotely. Polyglot. Startup experience.
LinkedIn Profile

By Me

Statistics & R - a blog about - you guessed it - statistics and the R programming language.
R-blog

Erlang Explained - a blog on the marvelllous programming language Erlang.
Erlang Explained