[erlang-questions] Erlang for youngsters
Torben Hoffmann
torben.hoffmann@REDACTED
Mon Jun 16 14:18:16 CEST 2014
Anthony Ramine writes:
> Did anyone ever wonder whether Erlang is truly hard to learn, or if it is just how fault-tolerant, concurrent, distributed programming is by definition?
Good question!
I think that fault-tolerant, distributed programming is hard because there is a big
context you need to address. The sheer about of things that can go wrong will make it
difficult to get into.
I think that concurrent programming is easy to learn:
-------------------------------------------------------
I will start teaching your how to program in Erlang by showing you how the
fundamental building block of Erlang, a process works.
A process can receive and send messages. Sending messages is the only way two
processes can communicate.
Here's a simple process that just acknowledges that it has received a message and
continues to do so over and over again:
echo() ->
receive
Msg ->
io:format("I'm happy, 'cause I recevied: ~p~n", [Msg]
end,
echo().
The file example1.erl contains the echo function.
Open up the Erlang shell:
$ erl
c(example1).
(this compiles the code and now you can start an echo process)
Pid = spawn ( fun() -> echo() end ).
(you just stored the process identifier (Pid) for the process you spawn, so now you
can send it a message:)
Pid ! "Erlang is cool".
"I'm happy, 'cause I received: Erlang is cool"
-------------------------------------------------------
Then one can add more layers to this, step by step.
Pattern matching can be sneaked in very fast. We already have recursion going for us.
Create a counter process and you have recursion with state data for a process.
For a fresh mind it will be okay to have processes crash. They know of nothing else.
But the key point is still to find a "killer" example that they find motivating.
Cheers,
Torben
--
Torben Hoffmann
CTO
Erlang Solutions Ltd.
Tel: +45 25 14 05 38
http://www.erlang-solutions.com
More information about the erlang-questions
mailing list