Erlang

起步

% 注释
2 + 2.0.
"str".
[1, 2, 3].
Var = 1. % 定义变量

原子 列表 元组

Color = red. % red是一个原子 赋值给Color
[1,2,"ds"]. % 列表是异质变长的
{1, 2 , "3"}. % 元组是变长的

模式匹配

{X, Y} = {1, 2}.
[One|Two = [1,2,3,4]. % Two 匹配剩下的那些
All = <<W:4, Z:4>>. % 位匹配
<<A:4, B:4>> = All.

函数

-module(basic).
-export([mirror/1, number/1]).
-export([sum/1]).

mirror(Anything) -> Anything.
number(one) -> 1. % 模式匹配

sum(0) -> 0;
sum(1) -> 1;
sum(N) -> N + sum(N-1). % 递归

控制结构

Animal = "dog".
case Animal of
  "dog" -> "bark";
  "cat" -> "mow";
  _ -> "unknow"
end.
X = 0.
if
  X >=0 -> positive;
  X <0 -> negative
end.

匿名函数

Add = fun(A, B) -> A+B end.
Add(1,2).

列表与高阶函数

Numbers = [1,2,3].
lists:foreach(fun(I) -> io:format("~p~n",[I]) end, Numbers). % 迭代
lists:map(fun(I) -> I * I end, Numbers). % 映射\

lists:filter(fun(I) -> I > 1 end, Numbers).
lists:all(fun(I) -> I > 1 end, Numbers).
lists:any(fun(I) -> I > 1 end, Numbers).

lists:foldl(fun(I,SUM) -> I + SUM end, 0, [1,2,3,4,5]). % reduce

并发

-module(translate).
-export([loop/0]).

loop() ->
  receive % 接收消息
    "chinese" ->
      io:format("中文"),
      loop();
    "dog" ->
      io:format("狗"),
      loop();
    _ ->
      io:format("未知"),
      loop
end.
Pid = spawn(fun translate:loop/0). % 启动轻量级进程
Pid ! "chinese". % 发送消息
-module(translate).
-export([loop/0,translate/2]).

loop() ->
  receive
    { Form, "chinese"} ->
      Form ! "ccc",
      loop();
    { Form, "dog"} ->
      Form ! "ddd",
      loop();
    { Form, _ } ->
      Form ! "uuu", % 发送
      loop
end.

translate(Pid, Word) ->
  Pid ! {self(), Word}, % 同步发送
  receive
    Trans -> Trans
  end.
translate:translate(Pid, "dog").

核心优势

不足