Station R2 · Optional reviewOpen the adventure map
00Set up your toolsSetup01The starting lineSetup02Processes and mailboxesR1An Elixir data pipelineOptional reviewR2Read ErlangOptional review03Messages and timeouts04OTP rules for messages05A supervision tree that restarts06Put a limit on concurrency07When a BEAM node disappearsX1Agree on shared BEAM termsBilingual extensionX2Two-language partnersBilingual extension08A reliable job runner
Home/BEAM mainline/Station R2
R2
LanguageBeginner explorationOptional reviewErlang

Read Erlang

Begin with uppercase and lowercase names, then follow the signs made by commas, semicolons, and periods.

5checkpoints
About 6 hours · Try 5 sessionssplit it into sessions
QUESTION · ONE PROBLEM FOR THIS STATION

[Optional language review] How can we rebuild the same log tool in Erlang and make both implementations obey one input-output contract?

Start at the scene

Both implementations passed their own tests but returned different results

The Elixir and Erlang parsers each pass their own tests, yet disagree on blank lines, WARN, and malformed input. The cause hides in exports, punctuation, patterns, and input assumptions.

Evidence you can observe
  • Shared input fixtures and expected results for both implementations
  • The exported function list and the compiler's first error
  • Side-by-side output for blank, INFO, WARN, and malformed inputs
Why this station matters

The problem it solves

Many BEAM documents and tools use Erlang. Once you can read its basic forms, error messages, :gen_tcp, and Observer become easier to understand. Start with data, functions, and punctuation.

After this station

You will be able to

  • Recognize Erlang atoms, variables, tuples, lists, maps, and binaries

  • Use commas, semicolons, and periods to show “continue,” “try another path,” and “end here”

  • Create a Rebar3 project and use EUnit to check your functions

Before you start
  • Finish the Elixir foundations review and understand the log-processing steps from the last station
  • Know that pattern matching checks a data shape, and have seen a function call itself again
One job, two ways to write it

See what it does before how it is written

An Erlang module name is an atom. The 1 in total/1 says the function takes one argument. A list comprehension uses shape patterns to keep paid orders and then take out their amounts.

Current code
Elixir
# Add the amounts from all paid orders
defmodule Orders do
  def total(items) do
    # Filter by status, take out the amounts, then add them
    items
    |> Enum.filter(&(&1.status == :paid))
    |> Enum.map(& &1.amount)
    |> Enum.sum()
  end
end
LAB
Hands on

Punctuate some Erlang code

About 15–25 minutes

Break a comma, semicolon, and period one at a time. Do not memorize the errors. Find the part the compiler thinks has not ended.

  1. 01

    Create classify/1 with one clause for positive numbers and another for zero or negative numbers

  2. 02

    Change the semicolon at the end of the first clause to a period, then compile again

  3. 03

    Inside one function body, change a comma that means “continue” into a semicolon, then compile again

Copy into the terminal and press Enter
# Compile the project and run every EUnit test
rebar3 eunit
What you should see
  • A semicolon separates clauses with the same function name and arity
  • A period tells the compiler that the whole function definition ends here
  • A comma continues to the next expression in the same function body
Break it on purpose

Remove -export([total/1])., then call the function from the shell. The function still exists in the module, but outside callers cannot reach it.

What this shows

Erlang commas, semicolons, and periods express code structure. They cannot be exchanged just for appearance.

What this does not show yet

Successful compilation only shows that the basic structure is valid. It does not prove that the answer is correct.

Meet the words

Key ideas in this code

01

Uppercase variable

In Erlang, a name beginning with an uppercase letter or underscore is a variable. A bare lowercase word is usually a fixed label called an atom.

02

Punctuation structure

A comma joins expressions in order. A semicolon separates function clauses or branches. A period ends a full definition or shell expression.

03

binary

A container for a sequence of bytes, written <<...>>. UTF-8 text usually lives in a binary. A traditional charlist is instead a list of integers that represent characters.

Name the shape in the code

Design patterns used here

01

Contract Test

Use the same fixtures to constrain both languages' inputs and outputs.

02

Ports and Adapters

Keep language-specific implementations behind a shared contract.

Think it through

What does Erlang treat the unquoted name user_name as?

Your turn

Translate a program into Erlang

Rebuild the log tool with Rebar3. Make the Erlang and Elixir versions read the same examples and return the same results.

Hint 1Take the first step

Use binary shape patterns to take apart INFO, WARN, and ERROR prefixes.

Hint 2Make it a little smaller

Put file reading in a separate module. Let the core log function receive only a list of binaries.

Hint 3You are close

Bring the test cases from the last station. EUnit tests can use _test functions or generator forms.

Hint 4Work backward from the finish

Choose one success signal and write the smallest test for it. If the computer cannot show the result, rewrite the signal as something you can truly observe.

Ready to move on when
  • The module exports only the functions that outside callers truly need

  • The code clearly separates binaries and charlists and converts when needed

  • EUnit checks the same normal, edge, and error cases as the Elixir version

Take these with you

Remember three things

  1. 1

    Erlang writes out a module, function, and arity. Together they act like the function's full address.

  2. 2

    A binary and a charlist are different data. When the two languages exchange text, inspect and convert it on purpose.

  3. 3

    Commas, semicolons, and periods are structural signs, not decoration.

Read a little more

Visit the original sources

Erlang Reference ManualRebar3EUnit
Station completeYou ran the experiment and thought through the answer. Save this station.