Erlang - Foundation

www.erlang.org


Cmd


erl: command line
-boot [boot(config) file name without extension] (.rel -> .script -> .boot by systools[sasl] module functions)
-mode interactive[default]/embedded
-pa directories: add directories to the head of the code search path.
-pz directories: add directories to the end of the code search path. (refer to code[kernel] module functions)
-sname subDomainName: give a node name to this Erlang system instance
-name FullQualifiedDomainName: give a node name to this Erlang system instance
-s Module Method Param: execute apply(Module, Method, [Param]), can be add serveral times and will execute in order
-noshell: execute and exit, don’t enter in interactive mode

werl: window command line

erlc erlFileWithExtension: compile .erl file to .beam file
c(erlFileWithoutExtension, [options]): compile .erl file to .beam file and load the code
c(m1, {d, ‘macro’}).: define a macro and compile the code (the macro is used for conditional compilation)
compile:file(erlFileWithoutExtension, [options]): compile .erl file to .beam file
compile:file(erlFileWithoutExtension, ['P']): just preprocess the code and output it to erlFileName.P

code:get_path(). % get search paths

.: period a sentence
;: separate clause (or)
,: and expressions

Home folder:
init:get_argument(home).

Config:
.erlang at $HOME folder: global start script, could place any erlang function in it
.erlang at any folder: specified start script, will override the global start script if curren folder has one

Exit:
q().: shorthand for init:stop()
erlang::halt().
init:stop().
init:restart().
Ctrl+g, q [Enter]

v(N): view line Nth history result

cd(“path”):
c:cd(“path”): change directory

pwd():
c:pwd(): print working directory
file:get_cwd(): get currend working directory

Manual:
erl -man [module] % unix/linux only

Comment%:
A comment starts with a % character and goes on to the end of the line

-compile(export_all). % exports all the functions in the module

erl -man Module: show Module man page, only supports Linux/Unix. The man can be download from here and must place in the root of the Erlang installation directory (usually /usr/local/lib/erlang).

webtool:start().: Web tools, includes an erl_crash.dump analyzer.

nl(ModuleName): c:nl(Module), Loads Module on all connected nodes.

net_adm:ping(Node): ping another Node


Variable:


Variables must start with a capital letter: N, Age, ShoeSize
A variable can only be given a value once in its context (scope)
unbound variable & bound variable

anonymous variable “_”.
“_VarName”, is normal variable, but no alter if it only occur onece.

f(): BIF, forget any variable binding (unset/unbind variables)

-Number: integer or floating-point number

dollar syntax($)
I = $a % I = 97
[I-32,$u,$r,$p,$r,$i,$s,$e]. % “Aurprise”

N#[number]: N could be 2~36

/: always returns a floating-point number
div: integer division and always returns a integer
rem: integer remainder and always returns a integer

-Atom(”): represent different non-numerical constant values. Start with lower letter and consist of alphanumeric or underscore (_) or at(@) sign, or quoted with single quotation mark(‘)
johnson, joe@gmail.com, _abc, ‘It is Good’, ‘+’

-Tuple({}):
P = {joe, 1.82}. % contains a atom and a floating-point number
Person={person,{name,{first,johnson},{last,lau}},{footsize,42}}.

{person,{name,{first,Who1},{last,lau}},{footsize,42}}={person,{name,{first,johnson},{last,lau}},{footsize,42}}.
% Who1=johnson

{_,{_,{_,Who2},_},_} = Person.
%Who2=johnson, _ is anonymous variable

element(N, Tuple): BIF, get the Nth(1 base) Tuple element

-List([]):
ThingsToBuy = [{apples, 10}, {pears, 6}, {milk, 3}].
% the {apples, 10} is called the head of the list, the other parts is called the tail of the list

ThingsToBuy2 = [{oranges, 4}, {newspaper, 1} | ThingsToBuy].
%define another list, the “ThingsToBuy” must be a list (proper formed)

[Buy1|ThingsToBuy3] = ThingsToBuy2.
% Buy1={oranges, 4} and ThingsToBuy3= [{newspaper, 1}, {apples, 10}, {pears, 6}, {milk, 3}]

[Buy2, Buy3|ThingsToBuy4] = ThingsToBuy3
% Buy2={newspaper, 1}, Buy3={apples, 10}, ThingsToBuy4= [{pears, 6}, {milk, 3}]

List comprehensions:

List = [{a, 1, 2}, {b, 5, 6}, {a, 3, 4}, {a, 7, 8}].
[X*Y || {a,X,Y} <- List, X >= 2, Y>=2]. % [12, 56]

++: [1,2] ++ [3,4] %[1,2,3,4], inefficient and not recommended unless it is small list
: [1,2,4,1,4] — [3,4] %[1,2,1,4], inefficient and not recommended unless it is small list

String(“”):
Strings are really just lists of integers. it is just a shorthand for the list of integers
Name = “abc”. % [97, 98, 99]

For erlang, string is a just a list of integers(any bytes, not just 1 byte. e.g. Chinese GB2312, will use 2 bytes for one character) in some encoding.


Processes:


Threads of execution in Erlang share no data, that’s why we call them
processes.

- Pid ! Message:
- {Register_name, Node_name} ! Message:
Send Message to process. It won’t cause error even Node_name nor Register_name doesn’t exist. It returns the sent Message. Erlang Pids contain information about where the process executes so if you know the Pid of a process, the “!” operator can be used to send it a message if the process is on the same node or on a different node.

- self(): get current process Pid


Record:


-record(todo, {status=reminder,who=joe,text}). % records should be defined in .hrl file

rr(“records.hrl”). % record read into shell from .hrl file
rf(record_name). % record forget in shell env

X1 = #todo{status=urgent, text=”Fix errata in book”}.
X2 = X1#todo{status=done}. % copy record X1 and override some value

X2#todo.text. %extract a value in a record


References:


erlang:make_ref(): can generate a unique identity.