函数
mirror(Anything) -> Anything.
number(one) -> 1;
number(two) -> 2;
number(three) -> 3;
number(_) -> default.
another_factorial(0) -> 1;
another_factorial(N) -> N * another_factorial(N-1).
another_fib(0) -> 1;
another_fib(1) -> 1;
another_fib(N) -> another_fib(N-1) + another_fib(N-2).
模式匹配
animal_case(Animal) ->
case Animal of
"dog" -> underdog;
"cat" -> thundercat;
_ -> unknown
end.
my_abs(N) ->
if
N > 0 -> positive;
N < 0 -> negative;
true -> zero
end.
actor并发
loop() ->
receive
{From, "casa"} ->
From ! "house",
loop();
{From, "blanca"} ->
From ! "white",
loop();
3 ->
io:format("bang~n"), exit({basic, dir, at, erlang:time()});
{From, _} ->
From ! "I don't understand.",
loop()
end.
translate(To, Word) ->
To ! {self(), Word},
receive
Translate -> Translate
end.
输出结果
1> Pid = spawn(fun translate:loop/0).
<0.21.0>
2> Pid ! "casa"
"house"
"casa"