Erlang Notes – Variables and assignment

Here is the second of some notes about erlang.

Obviously this is a very cursory glance at things. It’s really just notes to help me remember things. If you’re looking for something in more detail you can take a look at the erlang reference manual.

Variables

  • Literals starting with an upper case character, such as Pizza are variables
  • Variables are single assignment only, meaning that once they are bound to a value, that value cannot be changed. That’s different from a lot of languages, obviously, where we can change the values of variables during the run of the program. For example:

    1> % Lets assign X to be 6
    1> X = 6.
    6
    2> X = 7.
    ** exception error: no match of right hand side value 7

    When trying to re-assign a new value to a variable, we get an error message. The error message complains about bad matching, which leads us into the next issue.

Assignment

Assignment is slightly different from traditional languages. It is done by pattern matching the RHS against the LHS of an expression, rather than the more traditional manner of simply plugging the value of the RHS into the LHS.

Numbers

Pattern matching a number is simple. If the number or expression on the RHS evaluates to the same number as on the LHS, then there is a match. For example:

1> X = 6.
6
2> Y = 2.
2
3> X = 7.
** exception error: no match of right hand side value 7
4> X = Y.
** exception error: no match of right hand side value 2
5> X = Y + 4.
6

Tuples

Matching tuples is a little more complicated. Of course, you can have a direct match between two tuples, but more commonly, you can extract values from tuples by pattern matching. Lets say we have a tuple structure that represents an employee and we want to extract the employee name

2> % First we'll define a tuple
2> Employee = {employee, {name, grant}}.
{employee,{name,grant}}
3> % Now we'll pattern match on the tuple to extract the
3> % particular element we want from the tuple
3> {employee, {name, EmployeeName}} = Employee.
{employee,{name,grant}}
4> % So we expect that the EmployeeName variable
4> % will have the value grant now.
4> EmployeeName.
grant

Interestingly, this seems to reverse the common pattern of accessing elements deeply nested in structures in more conventional languages. In ruby, for instance, we might have an example like this:

person = {:employee => {:name => 'grant'}}
employee_name = person[:employee][:name]

In this case we have a naked variable name on the LHS and do some silly walking on the structure on the RHS to get at the value we want. Erlang reverses this so that we have a naked structure (meaning no function calls or other manipulation) on the RHS and we pattern match our way to the value that we want on the LHS of the expression.

Lists

Pattern matching Lists seems to be a little more simple. In general, a list can be pattern matched by the form [A, B, C... | T] where A, B and C are variables substituted by the first, second and third elements of the list respectively, and T is substituted by the remaining elements of the list. For example:

1> L = [1,2,3,4,5].
[1,2,3,4,5]
2> [A,B,C | T] = L.
[1,2,3,4,5]
3> A.
1
4> B.
2
5> C.
3
6> T.
[4,5]
7> [A, B, C, D, E, F, G] = L.
** exception error: no match of right hand side value [1,2,3,4,5]

Note that if we run out of elements on the RHS of the expression we’ll get a bad match type message. Also, the most common operation with this type of matching is more than likely to remove the head of the list into a variable so that we can work with it, and store the tail of the list for future work.

Leave a Comment