Hello World in Elixir
Your first Elixir program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first Elixir program.
The Code
Create a file named hello.exs:
| |
That’s it! One line to print to the console.
Understanding the Code
IO.puts()- Prints a string to standard output with a newline.IOis a module,putsis a function..exsextension - Elixir script file. Interpreted directly without compilation.- Parentheses optional -
IO.puts "Hello, World!"also works, but explicit parens are clearer.
Running with Docker
The easiest way to run this without installing Elixir locally:
| |
Running Locally
If you have Elixir installed:
| |
Or use the interactive shell (IEx):
| |
Expected Output
Hello, World!
Key Concepts
- Functional language - Functions are the primary building blocks
- Immutable data - Values can’t be changed after creation
.exsvs.ex- Scripts (.exs) vs compiled modules (.ex)- Module.function() - Functions live in modules (like
IO.puts) - BEAM VM - Runs on the Erlang virtual machine
.exs vs .ex Files
.exs- Script files, interpreted each time, used for scripts and tests.ex- Source files, compiled to bytecode, used for application code
For quick scripts like Hello World, .exs is perfect.
A More Elixir-Style Example
| |
This demonstrates:
defmodule- Defines a module (container for functions)def- Defines a public function- String interpolation -
#{expression}embeds values in strings - Implicit return - The last expression is returned automatically
Pattern Matching Example
Elixir’s pattern matching is powerful:
| |
Different function clauses match different inputs—no if statements needed!
The Pipe Operator
Elixir’s pipe operator (|>) chains function calls:
| |
Data flows left to right, making transformations readable.
Interactive Elixir (IEx)
IEx is great for exploration:
| |
The h helper shows documentation for any function right in the shell.
Next Steps
Continue to Variables and Pattern Matching to learn about Elixir’s powerful pattern matching and immutable variables.
Running Today
All examples can be run using Docker:
docker pull elixir:1.17-alpine