Hello World in Erlang
Your first Erlang program - the classic Hello World example with Docker setup using escript
Every programming journey starts with Hello World. Let’s write our first Erlang program and explore what makes this concurrent functional language tick.
The Code
Create a file named hello.erl:
| |
This is an Erlang script (escript) - the simplest way to run Erlang code.
Understanding the Code
Let’s break down this short program:
The Shebang Line
| |
This tells Unix-like systems to run the file with escript, Erlang’s scripting tool. It’s optional when running with escript hello.erl directly but makes the file executable on its own.
The Main Function
| |
main(_)- The entry point for escripts. The_means we’re ignoring the command-line arguments->- Separates the function head from the bodyio:format/1- Calls theformatfunction from theiomodule"Hello, World!~n"- The format string, where~nis a newline.- Ends the function definition (like a statement terminator)
Erlang’s Syntax Quirks
Erlang syntax takes some getting used to:
| |
Running with Docker
The easiest way to run Erlang without installing anything locally is with Docker:
| |
Understanding the Docker Command
docker run --rm- Run a container and remove it when done-v $(pwd):/app- Mount the current directory to/appin the container-w /app- Set the working directory to/apperlang:alpine- Use the official Erlang Docker image (Alpine-based, smaller)escript hello.erl- Run our script with escript
Expected Output
Hello, World!
Alternative Approaches
Erlang offers several ways to write and run programs. Let’s explore them.
Traditional Module Approach
For larger programs, you’d use a proper module:
| |
Run it in the Erlang shell:
| |
Or interactively:
| |
| |
Module Declaration Explained
| |
-module(name)- Declares the module name (must match filename without.erl)-export([...])- Lists functions visible from outside the modulehello/0- Functionhellowith 0 arguments (arity)
Using io:fwrite
| |
io:fwrite/1 is an alias for io:format/1.
Using Format Strings
Erlang’s format strings are powerful:
| |
Common format specifiers:
~s- String~p- Pretty print any term~w- Write any term (raw)~n- Newline~B- Integer in base 10
Multiple Expressions
| |
Note: Commas separate expressions within a function body.
Understanding Erlang Types
Even in Hello World, we encounter Erlang’s data types:
Atoms
| |
Strings
| |
Erlang strings are lists of characters:
| |
Binaries (Modern String Handling)
For efficient string handling, use binaries:
| |
The Erlang Shell
The Erlang shell (erl) is your interactive playground:
| |
Useful shell commands:
h().- Helpc(module).- Compile a modulel(module).- Load a modulem(module).- Module informationq().- Quit (graceful)halt().- Quit (immediate)
Pattern Matching Preview
Even simple programs hint at Erlang’s pattern matching:
| |
Run with:
| |
This shows:
- Multiple function clauses (separated by
;) - Pattern matching on arguments
[Head|Tail]list destructuring
Installing Erlang Locally
If you prefer to run Erlang without Docker:
macOS with Homebrew:
| |
Ubuntu/Debian:
| |
Using asdf (version manager):
| |
After installation:
| |
Common Beginner Mistakes
Forgetting the Period
| |
Wrong Module Name
| |
Confusing Semicolons and Periods
| |
Case Sensitivity
| |
Why Erlang for Hello World?
Even in this simple program, Erlang’s philosophy shows:
- Functional style -
io:formatis a function call, not a method - Explicit module system - We specify
io:format, not justprint - Pattern matching - The
_inmain(_)ignores arguments - Atoms for control - The shell returns
okatom to indicate success
Historical Note
Erlang was developed at Ericsson starting in 1986 by Joe Armstrong, Robert Virding, and Mike Williams. The language was designed for building highly concurrent, fault-tolerant telephone switches. The name “Erlang” honors mathematician Agner Krarup Erlang, who pioneered telephone traffic engineering.
Today, Erlang powers systems requiring extreme reliability:
- WhatsApp (billions of messages daily)
- Discord (real-time communication)
- RabbitMQ (message broker)
- CouchDB (database)
Next Steps
Continue to Processes and Concurrency to learn about Erlang’s legendary concurrency model - the feature that makes it unique among programming languages.
Key Takeaways
escriptruns Erlang scripts without compilationio:format/1,2outputs formatted text with~nfor newlines- Periods end function definitions
- Module names must match filenames
- Atoms (lowercase) and variables (uppercase) are different
_is a throwaway pattern that matches anything- The Erlang shell (
erl) is great for experimentation