erl: the interactive shell

Start erl

Run erl with no arguments to start a local node of Erlang with a shell. The shell prints its version, then a prompt that counts the queries from one. Every answer is a value, and the next query may use it.

erl

Erlang/OTP 27 [erts-15.2.7.14] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns] Eshell V15.2.7.14 (press Ctrl+G to abort, type help(). for help) 1>

The prompt accepts one expression at a time, and the expression is finished by a full stop, not by the line break. A line without the period leaves the shell waiting: the prompt turns into 2>, 3> and so on for the continuation lines, and nothing is evaluated until the period arrives. Ctrl+G opens the user switch menu (the shell says so in its banner), Ctrl+C interrupts the node, and Ctrl+D at an empty prompt ends it.

Evaluate expressions

Any expression works at the prompt: arithmetic, an atom, a list. The shell prints the value of the expression, not the text that was typed.

erl

1> 1 + 2. 3 2> hello. hello 3> [hello, world]. [hello,world] 4> 2 * 3 + 4. 10

A lower-case name such as hello is an atom and evaluates to itself; a string is a list of character codes, which the shell prints in quotes, and ~p prints the codes instead. The Strings and binaries lesson takes the two forms apart.

A variable is bound once, so a second match with a different value is an error, not an assignment. The shell reports the error and keeps running:

erl

1> X = 5. 5 2> X + 1. 6 3> X = 6. ** exception error: no match of right hand side value 6 4> X. 5

Call a module

The shell is also the fastest way to try a function that lives in a file. A module compiles to a beam file, and the shell loads the beam from the current directory and calls the exported function by module:function(argument). Save this as geometry.erl:

geometry.erl

-module(geometry). -export([square/1, cube/1]). square(Side) -> Side * Side. cube(Side) -> Side * Side * Side.

Compile it with erlc, start the shell in the same directory, and call both functions. The period after the number is the end of the query, and the next prompt appears with its answer:

erlc geometry.erl

erl

1> geometry:square(3). 9 2> geometry:cube(3). 27 3> geometry:cube(geometry:square(2)). 64

A call to a module the shell cannot find answers ** exception error: undefined function geometry:square/1; the beam is missing from the directory or from the code path. The lesson Your first Erlang script writes the same module as a program that starts by itself.

Shell helpers

The shell has functions that are not Erlang library calls. help() lists every internal command, h() prints the history with the value each query returned, b() lists the variables bound in the shell, and q() stops the node cleanly. The history also answers to the up and down arrows.

erl

1> b(). ok 2> Name = "Ada". "Ada" 3> b(). Name = "Ada" ok 4> h(). 1: b() -> ok 2: Name = "Ada" -> "Ada" 3: b() -> ok ok 5> q(). ok

b() answers ok and prints the bindings above that line; h() prints one block per query and ends with ok as well. q() is init:stop(), so the node shuts down without an error.

Recall results

v(N) returns the value of query N, and a negative number counts back from the newest result, so v(-1) is the last one. A recalled value is an ordinary term and sits inside a longer expression.

erl

1> 2 + 2. 4 2> v(1). 4 3> v(-1) * 10. 40 4> v(1) + v(2). 8

e(N) is the neighbour of v(N): it runs the old query again instead of returning its value. The history of a shell is kept in memory only; the next lesson, The shell as a tool, goes through the helpers that save the most typing around a real program.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How does an expression end in the Erlang shell?

    Answer

    With a period

    The period, not the newline, tells the shell that the expression is complete.

  2. What does the shell answer to 1 + 2.?

    Answer

    3

    The shell prints the value of the expression, and 1 + 2 is 3.

  3. What does the shell print for [hello, world].?

    Answer

    [hello,world]

    The shell prints values without spaces after the commas, so the list appears as [hello,world].

  4. What is hello in the expression hello.?

    Answer

    An atom

    A lower-case name without quotes is an atom, and it evaluates to itself.

  5. Which function lists the internal commands of the shell?

    Answer

    help()

    help() prints the command list; the other three answer history, bindings and stored values.

  6. What does h(). show?

    Answer

    The history of queries and their results

    h() numbers the earlier queries and prints the value each one returned.

  7. What does q(). do?

    Answer

    Stops the node cleanly

    q() is shorthand for init:stop(), which shuts the node down without an error.

  8. What does this module print? -module(q1). -export([main/0]). side() -> 4. main() -> io:format("~p~n", [side() * side()]).

    Answer

    16

    side() returns 4, and 4 * 4 is 16.

  9. What does this module print? -module(q2). -export([main/0]). main() -> io:format("~p~n", [2 * 3 + 4]).

    Answer

    10

    Multiplication comes first, so the value is 6 + 4, which is 10.

  10. What does v(-1). return?

    Answer

    The value of the last query

    A negative number counts back from the newest result, so -1 is the one just printed.

The examples on this page were run with Erlang/OTP 27 (erts-15.2.7.14) on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Автор статьи: Arthur Isaev

Изображение баннера