Erlang

来自百合仙子's Wiki
跳转到导航 跳转到搜索

网络

交互使用示例:

> {ok,Sock} = gen_tcp:connect("baidu.com", 80, [{active,false}]).
{ok,#Port<0.589>}
2> gen_tcp:send(Sock, "GET / HTTP/1.1\r\n\r\n").
ok
3> gen_tcp:recv(Sock,0).
{ok,"..."}
4> gen_tcp:close(Sock).
ok
5>

对比

Erlang 的 Fibonacci 数列(原始算法)计算速度要比 Haskell 快一些(计算 fib(30) 时,ErlangRuby 版立刻给出了结果,Python 版需要约一秒,而 Haskell 版需要好几秒)。

-module(fib).
-export([fib/1]).

fib(N) when N > 2 ->
  fib(N-1) + fib(N-2);
fib(N) when N < 3 andalso N > 0 ->
  1;
fib(_) ->
  error.
fib 1 = 1
fib 2 = 1
fib x = fib (x-1) + fib (x-2)

运行

使用 escript 解释执行指定 Erlang 程序中的 main/1 函数。很慢!

运行已编译的 e3xp1 模块中的 main/0 函数,执行完后停止:

erl -noshell -s e3xp1 main -s init stop

参见

外部链接

教程与手册