Erlang: Getting Started Without Melting

There are two things that might be meant when someone references “Erlang”: the language, and the environment (the EVM/BEAM and OTP). The first one, the language part, is actually super simple and quick to learn. The much larger, deeper part is learning what the BEAM does and how OTP makes your programs better.

It is clear that without an understanding of Erlang we’re not going to get very far in terms of understanding OTP and won’t be skilled enough to reliably interact with the runtime through a shell. So let’s forget about the runtime and OTP for a bit and just aim at the lowest, most common beginners’ task in coding: writing a script that tells me “Hello, World!” and shows whatever arguments I pass to it from the command line:

#! /usr/bin/env escript

% Example of an escript
-mode(compile).

main(Args) ->
    ok = io:setopts([{encoding, unicode}]),
    ok = io:format("Hello, world!~n"),
    io:format("I received the args: ~tp~n", [Args]).

Let’s save that in a file called e_script, run the command chmod +x e_script to make it executable, and take a look at how this works:

ceverett@takoyaki:~$ ./e_script foo bar
Hello, world!
I received the args: ["foo","bar"]
ceverett@takoyaki:~$

Cool! So it actually works. I can see a few things already:

  1. I need to know how to call some things from the standard library to make stuff work, like io:format/2
  2. io:setopts([{encoding, unicode}]) seems to makes it OK to print UTF-8 characters to the terminal in a script
  3. An escript starts execution with a traditional main/1 function call

Some questions I might have include how or why we use the = for both assignment and assertion in Erlang, what the mantra “crash fast” really means, what keywords are reserved, and other issues which are covered in the Reference Manual (which is surprisingly small and quick to read and reference).

An issue some newcomers encounter is that navigating an unfamiliar set of documentation can be hard. Here are the most important links you will need to know to get familiar and do useful things with the sequential language:

This is a short list, but it is the most common links you’ll want to know how to find. It is also easy to pull up any given module for doing a search for “erlang [module name]” on any search engine. (Really, any of them.)

In the rare case that erlang.org is having a hard time I maintain a mirror of the docs for various Erlang release versions here as well: http://zxq9.com/erlang/

Start messing with sequential Erlang. Don’t worry about being fancy and massively concurrent or maximizing parallelization or whatever — just mess around at first and get a feel for the language using escript. It is a lot of fun and makes getting into the more fully encompassing instructional material much more comfortable.

2 thoughts on “Erlang: Getting Started Without Melting

  1. I’m java nerd. After diving several weeks in documentation it’s time to start project for evenings.

    1. Welcome to the world of incidental concurrency! I think you’ll enjoy it.
      Tooling is something that is a bit rough in the language for newcomers, so I wrote something a bit more dynamic that you might like called ZX.
      You can find it here: https://zxq9.com/projects/zomp/
      Based on that I’ve made some videos that start discussing projects from the bottom up and explain in close detail everything that is going on inside of a chat server and a Tetris implementation (yes, with decent tooling writing GUI stuff for client side is possible!).

      Tetris:
      https://rumble.com/vdp0b1-erlang-writing-a-tetris-clone-part-1-data-types-and-basic-gui-display.html

      Telnet chat server:
      https://rumble.com/ve8h9r-erlang-telnet-chat-server-part-1-getting-the-basics-out-from-scratch-cheati.html

      Dispatching Closures (OOP objects implemented in Erlang):
      https://rumble.com/vdpvfb-erlang-dispatching-closures-oop-style-object-implementation-in-fp.html

      Get in touch with us on IRC (FreeNode and OFTC both have #erlang channels).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.