时间:2023-01-17 12:46:26 | 栏目: | 点击:次
一、赋值时匹配
原子匹配
sum([1, 2, 3]). % 6
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"
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 Expression of
Pattern1 [when Guard1] -> Expression1;
Pattern2 [when Guard2] -> Expression2;
%% and so on ...
_ -> Expression3
end.
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
消息传递匹配