发布于 2016-03-02 08:44:49 | 143 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Erlang 编程语言

Erlang是一个结构化,动态类型编程语言,内建并行计算支持。最初是由爱立信专门为通信应用设计的,比如控制交换机或者变换协议等,因此非常适 合于构建分布式,实时软并行计算系统。


这篇文章主要介绍了Erlang中的匹配模式总结,本文讲解了赋值时匹配、流程控制中的匹配、消息传递匹配等内容,需要的朋友可以参考下

一、赋值时匹配

原子匹配



atom    = atom                        % atom

another = another                     % another

atom    = another                     % exception error


变量匹配


Var = 2.                              % 2

Var = 3 - 1.                          % 2

Var = 1.                              % exception error


元组匹配


Attr = {name, sloger}.                % {name, sloger}

{name, Name} = Attr.                  % {name, sloger}

Name.                                 % sloger


列表匹配


Langs = [perl, python, ruby, erlang].

[Head | Tail] = Langs.

Head.                                 % perl

Tail.                                 % [python, ruby, erlang]


参数匹配


sum([]) -> 0;

sum([H|T]) -> H + sum(T).

sum([1, 2, 3]).                       % 6


记录匹配


%% record(post, {title, slug, body, author}).

Post = #post{title = "Pattern Match in Erlang",
             slug = "pattern-match-in-erlang",
             body = "Bla bla bla...",
             author = sloger}.

#post{title = Title, slug = Slug} = Post.

Title.                                % "Erlang 中的模式匹配总结" Slug.                                 % "summary-of-pattern-match-in-erlang"


比特匹配


Red = 5.

Green = 23.

Blue = 200.

Color = <<Red:5, Green:6, Blue:5>>.

<<R1:5, G1:6, B1:5>> = Color.

R1.                                   % 5 G1.                                   % 23 B1.                                   % 200


二、流程控制中的匹配

if



if
    Pattern1 [when Guard1] -> Expression1;
    Pattern2 [when Guard2] -> Expression2;
    %% and so on ...
    _                      -> Expression3           % 匹配所有其它结果
end.


case

case Expression of
    Pattern1 [when Guard1] -> Expression1;
    Pattern2 [when Guard2] -> Expression2;
    %% and so on ...
    _                      -> Expression3
end.


try catch

try FunctionOrExpressions of
    Pattern1 [when Guard1] -> Expression1;
    Pattern2 [when Guard2] -> Expression2
    %% and so on ...
catch
    ExType:ExPattern1 [when ExGuard1] ->
        ExExpression1;
    ExType:ExPattern2 [when ExGuard2] ->
        ExExpression2;
    %% and so on ...
    _:_ -> DefaultExExpression               % _:_ 匹配所有异常
after
    AfterExpressions
end

消息传递匹配



loop() ->

    receive

        {From, {rectangle, Width, Height}} ->

            From ! {self(), Width * Height},

            loop();

        {From, {circle, R}} ->

            From ! {self(), 3.14 * R * R},

            loop();

        {From, _Other} ->

            From ! {self(), {error, unknown_shape}}

            loop()

    end.

Pid = spawn(fun loop/0).

Pid ! {self(), {rectangle, 10, 5}}.         % {Pid, 50}

Pid ! {self(), {circle, 4}}.                % {Pid, 50.24}

Pid ! {self(), {square, 10}}.               % {Pid, {error, unknown_shape}}



最新网友评论  共有(0)条评论 发布评论 返回顶部
推荐阅读
最新资讯

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务