Station R2 · Optional reviewOpen the adventure map
Read Erlang
Begin with uppercase and lowercase names, then follow the signs made by commas, semicolons, and periods.
[Optional language review] How can we rebuild the same log tool in Erlang and make both implementations obey one input-output contract?
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.
- 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
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.
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
- 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
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.
# 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
endPunctuate some Erlang code
Break a comma, semicolon, and period one at a time. Do not memorize the errors. Find the part the compiler thinks has not ended.
- 01
Create
classify/1with one clause for positive numbers and another for zero or negative numbers - 02
Change the semicolon at the end of the first clause to a period, then compile again
- 03
Inside one function body, change a comma that means “continue” into a semicolon, then compile again
# Compile the project and run every EUnit test
rebar3 eunit- 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
Remove -export([total/1])., then call the function from the shell. The function still exists in the module, but outside callers cannot reach it.
Erlang commas, semicolons, and periods express code structure. They cannot be exchanged just for appearance.
Successful compilation only shows that the basic structure is valid. It does not prove that the answer is correct.
Key ideas in this code
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.
Punctuation structure
A comma joins expressions in order. A semicolon separates function clauses or branches. A period ends a full definition or shell expression.
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.
Design patterns used here
Contract Test
Use the same fixtures to constrain both languages' inputs and outputs.
Ports and Adapters
Keep language-specific implementations behind a shared contract.
What does Erlang treat the unquoted name user_name as?
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.
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
Remember three things
- 1
Erlang writes out a module, function, and arity. Together they act like the function's full address.
- 2
A binary and a charlist are different data. When the two languages exchange text, inspect and convert it on purpose.
- 3
Commas, semicolons, and periods are structural signs, not decoration.