I assume you have already installed Erlang on your system and are able to run erlang shell either from command line or from within Emacs (C-c C-z).
This is a view of Eshell from my Emacs:
Erlang (BEAM) emulator version 5.5.2 [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.5.2 (abort with ^G)
1>
Let’s do a simple thing and try to assign a value to a variable in the Eshell. First you should know that variables in Erlang start with uppercase letter or underscore (_), for example:
- X
- Name
- Address
- _
- _NoWarn
Ok, let’s finally assign some value to a variable X:
1> X = 4. 4 2>
So far, so good. Make sure to type final “.” at the end of expression when you are done entering code.
Let’s assign new value to X:
2> X = 5. =ERROR REPORT==== 27-Aug-2007::00:23:02 === Error in process <0.29.0> with exit value: {{badmatch,5},[{erl_eval,expr,3}]} ** exited: {{badmatch,5},[{erl_eval,expr,3}]} ** 3>
Here you need to understand and remember couple of things:
- Erlang uses single assignment, a variable can only be bound once, bar none.
- In expression X = 5 operator = is not an assignment, but match operator and variables are bound to values using pattern matching mechanism.
I think it won’t be an exaggeration if I say that Pattern Matching permeates Erlang. To understand why we got ERROR REPORT in above example with {badmatch, 5} let’s dive into this pattern matching a bit.
When you use pattern matching’s = operator left side called pattern is matched against right side which is a term(piece of data of any Erlang’s data type: integer, float, list, tuple, etc). If your matching fails you get run-time error, if it succeeds then any unbound variable in lefthand pattern becomes bound.
Returning to our example – first we had an unbound variable:
1> X. ** 1: variable 'X' is unbound ** 2>
Then we bound value to variable X with pattern matching:
2> X = 4. 4 3> X. 4 4>
Now as the variable X is bound we can’t change its value any more (remember single assignment?) otherwise we’ll get badmatch error like in foregoing X = 5 (4 does not match 5), but we may use = to match our variable:
4> X = 4. 4 5>
Let’s add a bit of tuples to pattern matching. In Erlang tuple is defined inside {…}. ie:
5> {2, 3}. {2,3} 6>
and now pattern matching:
6> {X, Y} = {2, 3}. =ERROR REPORT==== 27-Aug-2007::01:10:45 === Error in process <0.29.0> with exit value: {{badmatch,{2,3}},[{erl_eval,expr,3}]} ** exited: {{badmatch,{2,3}},[{erl_eval,expr,3}]} ** 7> {X, Y} = {4, 3}. {4,3} 8> X. 4 9> Y. 3 10>
In first attempt we tried to match X with 2 and the matching failed as the value of X is 4, in a second attempt the matching succeeded and unbound variable Y also got its value (became bound).
This may look to you like tuple unpacking in Python:
>>> x, y = (4, 3) >>> x, y (4, 3) >>> x, y = (5, 3) >>> x, y (5, 3)
but it’s only visual similarity, though i must admit, useful one.
That’s it for today, later you’ll see how pattern matching is uber-useful and how it is used in different parts of Erlang’s constructs.



{ 4 comments… read them below or add one }
Nice and quick introduction. I leant few new things about Erl.
1. variables start with Caps or _
2. pattern matching
3. similarities to py tuple matching.
Please continue the tutorials
Thanks for feedback, Pradeep.
I’ll keep posting new stuff.
Please continue this tutorial!
It doesn’t look like there’s a link to the next part, so here: http://ruslanspivak.com/2007/09/09/erlang-for-python-programmers-part-i/
{ 6 trackbacks }