Erlang basics · Lesson 01 (open contents)
Open erl
Run expressions in the Erlang shell and remember the final dot.
Copy into erl and press Enter after each line
Notice the dot at the end of every runnable input.
%% An addition expression returns a number
2 + 3.
%% Call the string module to make text uppercase
string:uppercase("cat").
%% Call the io module to print a line
io:format("hello, BEAM~n", []).2 + 3.returns5.string:uppercase("cat").returns"CAT".io:format(...)prints a line, then showsok. For now, read it as a “done” label.
Read from the first line down
- The final dot in
2 + 3.is not a decimal point. It ends the input. string:uppercase("cat").calls theuppercasefunction in thestringmodule.~nmeans a new line. The empty list[]says there are no extra values to put into the format string.
Symbols are not secret signs
erlEnter this in a terminal to open the Erlang shell.
1>This is the shell prompt. Enter only the code after it.
.Tell the shell: this input is complete, so run it.
Match each name to its meaning
erl
The terminal command that starts the Erlang shell.
expression
Code that produces a value when it runs, such as 2 + 3.
dot
A complete shell input or function definition in Erlang ends with ..
Why these forms are useful
erl opens the Erlang shell. Like IEx, it lets you try a small piece of code at once.
The Erlang shell waits for a dot . before it knows an input is complete. If you forget the dot, it keeps waiting instead of running the code.
1> and 2> at the start of shell lines are prompts and input numbers. Do not copy them as code.
Close the answer and try
Ask the shell to multiply 8 by 7, then turn beam into uppercase text.
%% Add the operator, and do not forget the dot
8 ? 7.
%% Finish the function name
string:____("beam").Target result: First you should see 56, then "BEAM".
Stuck? Read one hint
Multiplication uses *. The function in the example was uppercase.
After you run it, see one answer
%% The star means multiplication
8 * 7.
%% Every complete input ends with a dot
string:uppercase("beam").Think about it: You entered `2 + 3`, but the shell is still waiting. What is probably missing?
The final dot is missing. Add . and press Enter.
Remember these three lines
- 1
erlopens the Erlang shell. - 2
The shell is useful for trying short Erlang expressions.
- 3
A complete shell input ends with a dot.