Variables, comparisons and dynamic typing. (Learning Erlang 3)

Published on Mar 19, 2012

Other articles in the Learning Erlang series.

  1. Learning Erlang.
  2. Heads and tails, working with lists. (Learning Erlang 2)
  3. Modules and functions, Erlang building blocks (Learning Erlang 4).
  4. Module attributes and compilation (Learning Erlang 5).
  5. Playing with recursion, (Learning Erlang 6).
  6. Playing with the file module (part 1). (Learning Erlang 7)
  7. The file module (part 2). (Learning Erlang 8)
  8. String manipulation in Erlang. (Learning Erlang 9)
  9. Records. (Learning Erlang 10)
  10. Functional arrays. (Learning Erlang 11)

Note of caution. These are my notes while learning Erlang. You are welcome to follow along and use them as a guide. Please make sure to check the Erlang language site

Variables can only be bound once per scope. There is no such thing as global variables. (Great!)
You don’t need to declare variables before hand, you just create and assign at the same time.
Variables name need to start with an uppercase follow by any number of upper and lower case letters, numbers or the underscore, other characters are invalid.

Single assignment means that variables are immutables.

The assignment is done by matching an unbound variable to any valid Erlang expression. The match is done using the equal sign. If the variable was previously bound we say that we are doing a test instead of an assignment.

You can assign multiple variables at once, just provide the right pattern.

Erlang goes dynamic.

Not sure if this is true but apparently the reason Erlang use dynamic typing is because the guys that wrote the language originally, didn’t know how to write a type system and subsequent attempts to doing so failed due to some design decisions in the language itself.

I really don’t care at all why Erlang is dynamic. I think dynamic languages make things easier in 80% of the cases (maybe more). Ultimately that’s what it is so just relax

One of these things.

One of these things Muppets wiki

Taking decisions sometimes can overwhelm us, simple human beens, but computers are cold in their binary logic. Let’s see how Erlang compare values.

Equality

Notice lines 8 and 9, the double equal will check for equality but doing some type conversion. If you want strict equality use the equal colon equal operator instead.

Inequality

As with equality there is a not equal and a exactly non equal operator.

More, less, more or equal, less or equal.

Nothing strange here, just the less or equal symbol is actually equal or less.

Comparing between numbers, lists, atoms and tuples can be done using the same operators.

When comparing different types the following is taking in consideration:

  • number are less than atom
  • atom are less than reference
  • reference are less than func
  • func are less than port
  • port are less than pid
  • pid are less than tuple
  • tuple are less than list
  • list are less than binary

Tuples are compared first by the number of elements contained and secondly each element is compared in turn.