Basic steps
Why Erlang, what is erlang, etc? Erlang is a programming language developed by Ericsson to provide a language that is functional to maximise efficiency (code reuse, higher order functions, see "Why is functional programming useful?") while at the same time allowing concurrency and distributed processing (vital in telecoms applications). Processes in Erlang are lightweight ie it takes little computational resources to create a new process and so programs can be thought of as consisting of a set of interacting "machines" that talk to each other by passing messages.
Start learning how to program in Erlang:
- install erlang from lang/erlang
- install erlang documentatiuon from lang/erlang-doc, this downloads the set of pdf files from the erlang website (incidentally, this is a good idea for a port, it would be helpful for learning other languages if the basic/seminal papers could be downloaded in a single step). Docs are installed to /usr/local/share/doc/erlang/
- Start with "Getting started with Erlang" (next section)
"Getting started with Erlang"
Basic types in erlang:
- Numbers: integers or floats. Integers are promoted to Bignums if they are too large, so things like fact(500) will work transparently (once you define a factorial function of course)
- Atoms: strings starting with a lowercase letter or anything inside single quotes eg 'This is an atom'
- Tuples: {thing1, thing2, ..., thing_n }
- Lists: store a variable number of items [a, b, 12]. Double quotes are shorthand for defining character code lists eg "abc" is [97,98,99]
- Variables: start with an uppercase letter
- More complex data types: tuples of tuples of tuples of (etc)
Modules are the basic compilation unit in erlang, ie define a module in a file module_name.erl by giving the module name and the functions the module exports, then define the functions. Functions that are not exported are private to the module.
Pattern matching can be used in function definitions eg a factorial module tut1:
%%% Code: tute1.erl
-module(tut1).
-export([fac/1]).
fac(1) ->
1;
fac(N) ->
N*fac(N-1).
%%% End Code: tute1.erl
Use this file in the interactive shell erl using "c(tut1)." to compile the file, then eg "tut1:fac(50)." to use the function
Points to note about erlang modules:
- functions having same names but different number of arguments are different functions. Nice for making tail recursive forms of functions.
- comments are started by % and go to end of line.
- get help on modules using erl -man
- in function definitions different cases are separated by semicolons, end of function definition by a period.
Processes in Erlang
One of the main driving forces behind erlang is its use in distributed environments. To facilitate this process creation is easy in terms of the computational resources required. Processes are distinct from threads in that threads can share data between themselves while processes have distinct local data. New processes are created using the built-in-function (BIF) spawn taking three arguments, the module name to spawn, the function within the module that is called when the module is spawned, and the list of arguments of the module.
The syntax for passing messages between processes is PID ! Message, where Message is any erlang term. If you want the receiving process to talk back to the sending process it is a good idea to send the PID of the sender along with any other data. Alternatively, the processes can be registered using register(some_atom, PID) so that the atom can be used as a process identifier rather than the PID itself.