François Pinard's site

Erlang summary


1   Code organisation

Modules in .erl should be compiled to .beam first. They contain functions ending with ., each containing one of many clauses separated with ;. Clauses have a head (pattern starting with a function name), the symbol ->, then a body (which is a sequence of expressions separated with ,).

2   Expressions

  • Numbers
    • $L yields the ordinal of ISO 8859-1 character L.
    • Floating numbers need at least one digit after point.
  • Atoms
    • Need to be [a-z][A-Z_a-z0-9@]*, or else be within apostrophes.
  • Variables
    • Have to be [A-Z_][A-ZA-z_0-9@]*.
    • _ is "don't care".
  • Tuples
    • {}
    • { EXPRESSION 【, EXPRESSION 】☼ }
  • Proper lists
    • []
    • [ EXPRESSION 【, EXPRESSION 】☼【 | PROPER_LIST】♭ ]
    • "CHARACTERS" for a list of (0 or more) ISO 8859-1 character ordinals. The following escapes are recognized:

      Escape    Meaning the ordinal of

      \b        Backspace

      \d        Delete

      \e        Escape

      \f        Form feed

      \n        New line

      \r        Carriage return

      \t        Horizontal tab

      \v        Vertical tab

      \NNN      N, NN or NNN are octal digits

      \^L       Ctrl-L, for any letter L

      \C        C for any other character

  • Lists
    • [ EXPRESSION 【, EXPRESSION 】☼【 | EXPRESSION】♭ ]

3   Operators

Here is a quick operator reminder chart. Each line gives operators sharing a precedence level. The first line gives the most binding operators, the last line gives the least binding operators. The last column indicates if operators are left associative () or right associative ():

  Operators                                     Arity Assoc

  :                                             2

  #                                             2

  +       -       bnot    not                   1

  /       *       div     rem     band    and   2     →

  +   -  bor bxor bsl bsr or  xor               2     →

  ++      --                                    2     ←

  ==  /=  =<  <   >=  >   =:=  =/=              2

  andalso                                       2     →

  orelse                                        2     →

  =       !                                     2     ←

  catch                                         1

A few explanations:

  Operators          Usage

  =                  Pattern matching

  ==  /=             Equality, for integer and real comparison only

  =:= =/=            Identity comparison

  andalso orelse     Short-circuit on booleans

For comparison operators::

  number < atom < reference < fun < port < pid < tuple < list < binary

4   Some attributes

Attribute                          Meaning

-module(NAME)

-import(MODULE, [FUNC/ARITY…])

-export([FUNC/ARITY…])

5   Some functions

Prototype     Meaning

help()        List all other shell commands