Erlang并发编程深入解析
1. 简单示例
在之前,我们定义过计算面积的函数 area/1 ,代码如下:
area({rectangle, Width, Ht}) -> Width * Ht;
area({circle, R}) -> 3.14159 * R * R.
现在,我们将这个函数重写为一个进程。以下是 area_server0 模块的代码:
-module(area_server0).
-export([loop/0]).
loop() ->
receive
{rectangle, Width, Ht} ->
io:format("Area of rectangle is ~p~n",[Width * Ht]),
loop();
{circle, R} ->
io:format("Area of circle is ~p~n", [3.14159 * R * R]),
loop();
Other ->
io:format("I don't know what the area of a ~p is ~n",[Other]),
loop()
end.
我们可以在shell中创
超级会员免费看
订阅专栏 解锁全文
57

被折叠的 条评论
为什么被折叠?



