Your first Erlang script

The module file

Every Erlang program starts with a module header. The name must match the file name, and the export list names the functions the outside world may call.

Here is the whole program. Save it as hello.erl.

-module(hello). -export([main/0]). main() -> io:format("Hello from Erlang~n").

Compile and run

Compile with erlc, which produces a hello.beam file, the compiled bytecode. Then start the shell, call the function, and quit.

erlc hello.erl
erl -noshell -eval 'hello:main(), halt().'

Hello from Erlang

The escript shortcut

The escript runner skips the compile step. Add a main function, mark the file executable, and run it directly.

The shebang line tells the system which interpreter to use, just like a shell script.

#!/usr/bin/env escript %%! -noshell -noinput main(_) -> io:format("Hello from escript~n").

chmod +x hello.escript
./hello.escript

Hello from escript

Why modules

The module header is not ceremony. It gives the compiler a name, the export list a boundary, and every later lesson a place to hang its code.

The arity in the export list is the argument count, so main/0 means main with zero arguments. Two functions may share a name when their arity differs.

Next: Variables and single assignment

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What must the name in -module(hello). match?

    Answer

    The file name, hello.erl

    erlc checks that the module attribute and the file name agree, so hello lives in hello.erl.

  2. What does -export([main/0]). allow?

    Answer

    main with zero arguments may be called from another module

    The export list names the functions and their arity; main/0 is main with no arguments.

  3. Which command compiles hello.erl?

    Answer

    erlc hello.erl

    erlc is the compiler driver; it writes hello.beam next to the source.

  4. Which file does erlc hello.erl produce?

    Answer

    hello.beam

    The compiler writes BEAM bytecode, which the runtime loads and runs.

  5. Which command runs hello:main() and then stops the node?

    Answer

    erl -noshell -eval 'hello:main(), halt().'

    -noshell skips the interactive shell, -eval runs the expression, and halt() ends the node.

  6. What is an escript?

    Answer

    A script Erlang compiles and runs directly

    The escript runner reads the script, compiles it in memory and calls main/1, so there is no erlc step.

  7. Which line names the interpreter at the top of an escript?

    Answer

    #!/usr/bin/env escript

    The shebang lets the shell start the file with the escript runner, exactly like a shell script.

  8. In main(_) -> ..., what does the underscore mean?

    Answer

    The argument list is ignored

    An escript calls main/1 with the command-line arguments; _ matches that list and binds nothing.

  9. Two functions in one module share a name. When is that allowed?

    Answer

    When their arity differs

    A function is identified by name and arity, so main/0 and main/1 are different functions.

  10. What does ~n do in io:format("Hello from Erlang~n")?

    Answer

    Prints a newline

    In a format string ~n stands for a newline, so the next output starts on a new line.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image