
Dabbling in Erlang, Part 2: A Minimal Introduction - pasxizeis
http://agis.kontext.gr/2013/10/12/dabbling-in-erlang-a-minimal-introduction.html
======
anonymouz
In the section "Functional programming for real" he states:

> Note that the order of the definitions is important: had we moved our base
> case at the bottom, the recursion would never finish because map(F, [H|T])
> would always match (remember? an empty list is also a list with a head and a
> tail).

Which confuses me a bit, since I am used to the empty list _not_ having a head
(I am thinking for example of Haskell). Is it true that in Erland the empty
list has a head? And what would it be? Some generic null object?

~~~
seiji
For readability, it's nice to have the base case at the top. For performance
(if performance is an issue) it's nice to have the base case at the bottom
since all function heads are evaluated in order.

If you're looping over a million element list and your base case is first,
it'll be checked a million times. If your base case is last, it'll only be
checked once when your normal cases no longer match.

And to clear up your confusion, the writeup is wrong about empty lists
matching on [H|T]:

    
    
       Eshell V5.10.1  (abort with ^G)
       1> [H|T] = [1, 2, 3, 4, 5].
       [1,2,3,4,5]
       2> [H2|T2] = [].
       ** exception error: no match of right hand side value []

~~~
mononcqc
The BEAM virtual machine is able to reorder most clauses based on need, and
for most cases, a binary search will be done through all clauses. Exceptions
include function clauses with guards, which cannot easily be reordered but
still impact whether a clause will be used or not.

The VM will instead try to reorder the clauses up until the next guard clause,
if possible.

You should mostly care about writing readable code and let the compiler and
the VM do the rest.

------
stiff
I have written a more quickly paced Erlang tutorial some years ago, it is not
even online anymore, but here is a copy from archive.org:

[http://web.archive.org/web/20080708140354/http://www.stifflo...](http://web.archive.org/web/20080708140354/http://www.stifflog.com/2007/05/09/erlang-
for-the-practical-man/)

I must shamelessly say it seems to have aged reasonably well, unlike some
other things I wrote I actually read it now with interest, especially I
haven't dabbled in Erlang since and forgot some details.

I recommend everyone who finds fun in programming to give Erlang a shot, I
don't think it's great for all the use cases (what is?), but, for example, for
writing servers of all kinds it's totally awesome, especially with OTP, which
I didn't cover in my tutorial. It's also completely different from everything
else, even if you know Prolog or Haskell/ML, which are related in some ways.
One day I would really like to get myself together and write something like a
game server for poker or bridge in it.

------
GrinningFool
This is a pretty good introduction and is pretty accessible on first pass
through.

However, a factual error:: "(remember? an empty list is also a list with a
head and a tail)."

An empty list is an empty list :) You could switch the order of those two
function heads and 'map' would still behave properly.

~~~
pasxizeis
Yes that was a mistake I made. I've fixed it now thanks to your feedback, with
a proper example.

------
robbles
For anyone finding the syntax of list comprehensions a bit confusing (like
me), I found this comparison to be useful if you're familiar with the more
readable (usually) Python equivalent:

    
    
        [expression(element) || element <- list, conditions]
    

is the same as

    
    
        [expression(element) for element in list if condition]

------
ritonlajoie
Is there some platform that offers such a VM as Erlang's one, but without the
functional language ?

~~~
petrohi
Erlang VM is built on the premise that it runs a functional language. I would
argue that it will be extremely inefficient to implement non-function language
to run on Erlang VM. By non-functional I mean language that is based on
mutable and shared state. You can look at Elixir ([http://elixir-
lang.org/](http://elixir-lang.org/)) to see great example of an alternative
language that runs natively on Erlang VM. While offering syntax that is more
familiar to many people Elixir retains all basic restrictions of Erlang.

------
appostolis
That's awesome mate keep up!!!

------
rdtsc
I like the pace, the formatting, and the content. Would love to see more!
Great work!

~~~
pasxizeis
Hey, thanks!

------
eddie_the_head
Two .gr / Greek links on HN in a day, nice.

~~~
rdtsc
One of main contributors to Erlang is Kostis Sagonas from Athens National
Technical University. He is one of the main contributors to HiPE (the native
code compiler), and creator of PropEr (property based tester).

