erlang第三天总结

1.cmd进入某个盘,比如F盘

直接f:


2.
-module(fac1).

%% ====================================================================
%% API functions
%% ====================================================================
-export([main/1]).



%% ====================================================================
%% Internal functions
%% ====================================================================

main([A]) ->
    I = list_to_integer(atom_to_list(A)),
    F = fac(I),
    io:format("factorial ~w = ~w~n", [I, F]),
    init:stop().

fac(0) -> 1;
fac(N) -> N*fac(N-1).

这里因为-s fac1 main 25这种方式,参数是[25]


3.windows下启动escript脚本

脚本代码如下:

#!/usr/bin/env escript
main([A]) ->
    I = list_to_integer(A),
    F = fac(I),
    io:format("factorial ~w = ~w~n",[I, F]).
    
fac(0) -> 1;

fac(N) -> N*fac(N-1).

在cmd进入代码目录,执行escript.exe factorial 25 (25是参数)


4. spawn的两种创建方式

Pid = spawn(Mod, Func, Args)

Pid = spawn(Fun)


5.超时值为0的接收

-module(area_server1).
-export([loop/0, rpc/2, test/0]).

rpc(Pid, Request) ->
    Pid ! {self(), Request},
    receive
        Response ->
            Response
    end.

loop() ->
    receive
        {From, {rectangle, Width, Ht}} ->
            io:format("rectangle is ~p~n",[Width * Ht]);
        {From, {circle, R}} ->
            io:format("circle is ~p~n",[R * R])
    after 0 ->
        receive
            Any ->
                io:format("hello")
        end
    end.

test() ->
     Pid = spawn(area_server1, loop, []),
     Pid ! {self,apple},
    % Pid ! {self(),{rectangle, 10, 8}},
     Pid ! {self(),banana},
     ok.

这里超时是指消息发送到邮箱后才开始计时,比如说after 0,消息到达邮箱后,会先执行receive,如果不匹配,再执行after 0.若是after 1000,消息到达邮箱后,判断1秒内是否匹配receive里面语句,如果不匹配,1秒后执行after 1000里面的语句

5.普通进程可以通过执行process_flag(trap_exit, true)变成系统进程

6.节点是一个独立的Erlang系统,包含一个自带地址空间和进程组的完整虚拟机。














猜你喜欢

转载自blog.csdn.net/boiled_water123/article/details/80429958