Erlang Hints

I’m reading excellent “Programming Erlang” by Joe Armstrong and find some new stuff I haven’t seen in “Erlang programming” by Francesco Cesarini. This is largely a self-note post, so take it with a grain of salt.


  • If your code prepends elements effectively reversing the order of the original list, you need to call lists:reverse(...) as opposed to concatenating lists (L ++ [ X ]). The reverse call is way more efficient and even though it’s defined in lists module, when VM sees the call, it invokes an internal version.

Exception handling

  • The value of the after block is lost
  • The after block can be omitted
  • If you don’t need any guards in the try ... catch block, you can use this shortcut:

    try Expr
  • To catch all exceptions, one needs to pattern-match _:_ in catch-block. Matching bare _ will assume catching only throws

General notes

  • Use export_all in development only. This is for two reasons. First, when you come to read your code later, you’ll know that the only important functions are the exported functions, and all the other functions cannot be called from outside the module so you can change them in any way you like, provided the interfaces to the exported functions remain the same. Second, the compiler can produce much better code if it knows exactly which functions are exported from the module.
  • Use match operators to avoid rebuilding atoms and other constructs that you’ve matched in the method signature:

    fun1([{user, Name, Age} = U|T]) ->
  • Data types ordering is as follows (any number is less than any atom etc.:

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

As I move on, I’ll be posting more of these self-notes…