The Erlang Shell & Your First Module
3.1 The shell
Every expression ends with a period . in the shell.
Useful shell commands:
list shell commands f(). / f(X). forget all / one variable binding compile and load mod.erl h(lists, map). documentation (OTP 23+) value of command N repeat command N load record definitions print term fully pwd()., cd(Dir)., ls(). print and clear shell mailbox
Ctrl+G opens the job control mode (h for help; i interrupt; s start new shell; c connect; q quit). Ctrl+C opens the BREAK menu (a to abort).
$ erl
Erlang/OTP 27 [erts-15.2] ...
1> 1 + 2.
3
2> X = 10.
10
3> X * 2.
20
4> q(). % quit (also halt(). or Ctrl+G then q)3.2 Comments
% Single-line comment (the only comment syntax)
%% Convention: two % for comments inside functions/sections
%%% Three % for module-level comments3.3 Hello World module
Or from the command line:
-module(hello). % module name must match the file name
-export([world/0]). % public functions: name/arity
world() ->
io:format("Hello, World!~n").1> c(hello).
{ok,hello}
2> hello:world().
Hello, World!
okerlc hello.erl
erl -noshell -s hello world -s init stop3.4 Escript
#!/usr/bin/env escript
%%! -smp enable
main(Args) ->
io:format("Hello, args: ~p~n", [Args]).escript hello.escript a b c3.5 Punctuation rules (the "Erlang grammar" for beginners)
separate expressions in a sequence separate clauses (function clauses, case branches) end a function (or a shell expression)
area({square, S}) -> S * S; % clause, more follow
area({circle, R}) ->
Pi = 3.14159, % expression, more follow
Pi * R * R. % end of function