Hello World
IO.puts("Hello, world!")
Run the script:
elixir main.exs
This prints a line in Elixir using the IO module. It is the normal first example for a script.
Variables
Values are immutable. Rebinding creates a new binding for the name.
name = "Dan"
count = 1
active = true
These bindings hold values for the current expression flow. In Elixir, you work with new bindings instead of mutating old values.
Pattern Matching
{:ok, name} = {:ok, "Dan"}
[first | rest] = [1, 2, 3]
This pulls values out of a tuple and a list by matching the shape on the left to the data on the right.
Functions
defmodule Greeter do
def greet(name) do
"Hello, #{name}!"
end
end
Greeter.greet("world")
This defines a small module with one function and then calls it. That is the standard way to group behavior in Elixir.