Вы находитесь на странице: 1из 1


Marouan OMEZZINE (marouan.omezzine[~at~]gmail[~dot~]com) http://www.myownpercept.com/2009/03/erlang-cheat-sheet/
S AMPLE MODULE Create an instance of a record i().information about the system.
-module(foo). M=#person{name="Marouan",surname="O"}. ni().information about the networked system.
-export([sayhi/0]). Access a single field in a record i(X,Y,Z). information about pid <X,Y,Z>.
% This is a comment. M#person.name. l(Module). load or reload module.
sayhi() -> Extracting multiple fields lc([File]). compile a list of Erlang modules.
io:format("Happy Erlanging!~n" ).
# person { name=A, surname=B } = M. ls(). list files in the current directory.
GETTING HELP STRING B ASICS ls(Dir). list files in directory <Dir>.
erl -man <module_name> Str = "The \\n escape sequence escapes a line" m().which modules are loaded.
CASE EXPRESSIONS lists:sort("CBDbaazerty").returns m(Mod). information about module <Mod>.
case Expression of "BCDaabertyz". memory(). memory allocation information.
Pattern1 [when Guard1] -> Expr_seq1; lists:subtract("abcd", "ab"). returns "cd". memory(T). memory allocation information of type
Pattern2 [when Guard2] -> Expr_seq2; lists:suffix(".mp3", "music.mp3"). returns true. <T>.
... lists:nth(1, "ABC"). returns 65 (1 is the index). nc(File). compile and load code in <File> on all
end length("ABC").returns 3. nodes
IF EXPRESSIONS lists:duplicate(5, $*). returns"*****" nl(Module). load module on all nodes
if string:chars($*, 5).returns "*****" pid(X,Y,Z). convert X,Y,Z to a Pid.
Guard1 -> lists:append(["Happy ", "Erlanging", pwd().print working directory.
Expr_seq1; "!"]).returns "Happy Erlanging!" q(). quit - shorthand for init:stop().
Guard2 -> ESCAPE SEQUENCE regs(). information about registered processes.
Expr_seq2; \b Backspace nregs(). information about all registered processes.
... \d Delete xm(M). cross reference check a module.
end \e Escape y(File). generate a Yecc parser.
TERM COMPARISONS \f Form feed ** commands in module i **
X > Y X is greater than Y. \n New line ih(). print help for the i module.
X < Y X is less than Y. \r Carriage return FUNCTIONS
X =< Y X is equal to or less than Y. \s Space Anonymous:
X >= Y X is greater than or equal to Y. \t Tab Z = fun(X) -> 2*X end.
X == Y X is equal to Y. \v Vertical tab Named functions:
X /= Y X is not equal to Y. \NNN \NN \N Octal characters (N is 0..7) cube(X) -> X*X .
X =:= Y X is identical to Y. \^a..\^z or \^A..\^Z Ctrl+A to Ctrl+Z BIF: BUILT-IN FUNCTIONS
X =/= Y X is not identical to Y. \’ Single quote BIF list:
IO \" Double quote http://www.erlang.org/doc/man/erlang.html
Io:format(“ I am ~s~n”, [String]). \\ Backslash D ATES AND TIME
Io:fwrite(“I am ~s~n”, [String]). \C The ASCII code for C (C is a character) (An {Date={Year,Month,Day},Time={Hour,
~n : new line | ~s : string |~f => float |~w : integer) Minutes,Seconds}} = erlang:localtime().
standard output |~p : like ~w but breaks after each ERLANG SHELL: {{2006,9,25},{4,34,29}}
line init:stop(). shutdown cleanly. b().
LIST erlang:system_info(version). the erts iversion Date = {2006,9,25}
Day = 25
ListA = [1,2,3,4]. init:script_id().the major Release version. Hour = 4
Returns [1,2,3,4] b(). display all variable bindings. Minutes = 34
ListB = [1+7,hello,2-2,{cost, apple, 30-20},3]. e(N). repeat the expression in query <N>. Seconds = 29
Returns [8,hello,0,{cost,apple,10},3] Time = {4,35,50}
f(). forget all variable bindings.
[H|T] = ListA. Year = 2006
Returns the Head and Tail.
f(X). forget the binding of variable X.
Comprehensions Summary : Analyzing the erl_crash.dump after a
history(N). set how many previous commands to
L = [ X*X || X <- [1,2,3] ]. crash.
Or lists:map(fun(X) -> X*X end, [1,2,3]) webtool:start().
results(N). set how many previous command results
This gives the output: [1,4,9] to keep. RUN _ERL
Simple Comprehensions v(N). use the value of query <N>. Summary : Redirect Erlang input and output
rd(R,D) . define a record. streams.
L = [ {X,Y} || X <- [1,2,3,4], Y <- [1,2,3,4],
X*X == Y]. rf(). remove all record information. run_erl [-daemon] pipe_dir/ log_dir
This gives the output: [ {1,1}, {2,4} ] rf(R). remove record information about R. "exec command [command_arguments]"
Permutations rl(). display all record information. FILE TYPES
rl(R). display record information about R. module .erl
[ [X]++[Y] || X<-"HT", Y<-"HT"]. include file .hrl
rp(Term). display Term using the shell's record
Returns ["HH","HT","TH","TT"] release resource file .rel
++ application resource file .app
rr(File). read record information from File
[1,2] ++ [2,3]. returns [1,2,2,3] (wildcards allowed). boot script .script
-- rr(F,R). read selected record information from file(s). binary boot script .boot
rr(F,R,O). read selected record information with configuration file .config
[1,2,3] --[2]. returns [1,3]
options. application upgrade file .appup
TUPLE release upgrade file relup
T= {1.9, 22, 3.99} ** commands in module c **
element(2, T) returns 22 (2 is the index) bt(Pid). stack backtrace for a process.
{_, Val, _} pattern matching to retrieve a value c(File). compile and load code in <File>.
cd(Dir). change working directory.
RECORDS flush(). flush any messages sent to the shell.
Erlang Run-Time System
Define a record
help() . help info.
-record( person, {name, surname} ).