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.
This 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.
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.
- 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.
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.
- 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 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
This is a bit of a challenge. Coming from other - especially non-functional languages - and being told that
0 == false evaluates to false, and
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.
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.
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.
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".
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".
-  Learn You Some Erlang - Starting Out - http://learnyousomeerlang.com/starting-out
-  Erlang Reference Manual - Data Types - http://erlang.org/doc/reference_manual/data_types.html
Other Erlang Resources
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.
Erlang mailing lists and forums
The Google group Erlang Programming
Erlang on Stack Exchange
Erlang on Freenode - Use #Erlang
Erlang on Slack
"Learn You Some Erlang For Great Good", by Fred Hebert
"Programming Erlang", by Joe Armstrong
The Zen of Erlang, by Fred Hebert. A partly practical, partly philosophical take on Erlang.
Ericsson's coding standard for Erlang - Programming rules and conventions.
Getting started with Erlang using IntelliJ IDEA (including Rebar3).