Erlang overview
Basic
Documentation: https://erlang.org/doc/search/
Functional language
Erlang is a functional language. Code need compile and running line by line.
Every line need finish by a .. like: A = 1..
Module
Every erlang file will consider as a module. You console will compile all module you want.
You have to add -module(<filename>). into first line. Module should be same with filename without suffix.
In erl console, run c(<filename>). to compile it. Run a function is like: <moduleName>: <functionName>(...<argument>)
Also you need export a list contained all function you expect to export. Like -export([append/2,distance/2, double/1, drop/2]). or -compile(export_all). to export all function.
append/2means a function called append with 2 arguments.
Variable & item
Variable a word starting with upper case. like A or Result.
item is a word starting from lower case. like request or add.
Variable store the value.
item use for matching.
Bind
In erlang, No concept about assigning a value to a variable. It is actually bind a variable to a value. Once they are bound together you cannot to assign a new value to it.
A = 1. # right
B = 2. # right
A = B, # wrong. Because A is already bound.Function
Sample function:
sample(A) -> A.The last line of Function is the return value. Use ; to represent end point of a branch.
if
(A == true) and (B == true) ->
true;
true ->
false
end.Multi-thread
Erlang use message passing module to deal with multi thread. Each thread have own mailbox, stack and heap.
thread id
Each thread have a id called Pid. You need use Pid as an address and sending the message through it.
receive and send message
echo() ->
receive
{echo, From, Ref, Msg} ->
From ! {responds, self(), Ref, Msg}, echo();
{stop} -> ok
end.spawn
Reference: http://erlang.org/doc/man/erlang.html#spawn-1
We need spawn/1, spawn/2, spawn/3 to generate new thread, usually we use spawn/3. The basic idea is giving a function that running initially at new thread. This function can calling another function inside the module.
Pid = spawn(?MODULE, echo, []). This line means we generate a new thread with a function called echo in unknown module (usually self module). The argument list is empty and the thread id bind with Pid.
special
fold
This is a basic function which is same as reduce() in javascript.
In standard library, erlang have lists:foldl(F,A,[H|T]).
foldl is folding the list by a given function from left to right. foldr is from right to left.
foldl(_F, A, []) -> A;
foldl(F, A, [H|T]) -> fold(F, F(H, A), [T]).
foldr(_F, A, []) -> A;
foldr(F, A, [H|T]) -> F(H, foldr(F, A, T)).
_FmeansFis unnecessary since first part of function not useF.
[H|T]is unique characteristic of erlang.His the first element.Tis the rest. [H|T] = [1,2,3,4,5].His 1.Tis [2,3,4,5]
We can run this function:foldl(fun (H, A) -> H + A end, 0 , [1,2,3,4,5]).
MakeFile
Sample makefile from a erlang homework:
all:
erlc *erl
erl -eval "lexgrm:start(), halt()" -noshell -detached
run: all
erl
clean:
rm -f *beam grm.erl lex.erl