【问题标题】:Define Guard Sequence And What Are The Valid Guard Expressions?定义保护序列以及有效的保护表达式是什么?
【发布时间】:2021-06-27 01:16:20
【问题描述】:

我想知道 Guard 序列以及 Erlang 中有效的 Guard 表达式是什么?

【问题讨论】:

  • 欢迎来到 Stack Overflow。这些问题似乎有点太宽泛了。文件怎么说?你哪里有问题?

标签: erlang expression guard


【解决方案1】:

Guard Sequences

保护序列是由分号 (;) 分隔的保护序列。 如果至少有一个保护为真,则保护序列为真。 (这 剩余的守卫(如果有的话)不会被评估。)

Guard1;...;GuardK

例子:

go(X,Y) when X==1;Y==2 ->
   io:format("Yes~n").

在外壳中:

2> a:go(1, 2).
Yes
ok
3> a:go(1, 3).
Yes
ok
4> a:go(4, 2).
Yes
ok
5> a:go(4, 3).
** exception error: no function clause matching 
                a:go(4,3) (a.erl, line 4)

所以,分号就像OR

守卫是一系列守卫表达式,用逗号 (,) 分隔。 如果所有保护表达式的计算结果为真,则保护为真。

GuardExpr1,...,GuardExprN

例子:

go(X,Y,Z) when X==1,Y==2, is_atom(Z) ->
    io:format("Yes~n").

在外壳中:

8> a:go(1, 3, a).
** exception error: no function clause matching 
                    a:go(1,3,a) (a.erl, line 4)
9> a:go(1, 2, a).
Yes
ok

所以,逗号就像AND

Guard Expressions

有效保护表达式集是有效保护表达式集的子集 Erlang 表达式。限制有效集合的原因 表达式是保护表达式的评估必须是 保证没有副作用。有效的守卫表达式是 以下:

Variables
Constants (atoms, integer, floats, lists, tuples, records, binaries, and maps)
Expressions that construct atoms, integer, floats, lists, tuples, records, binaries, and maps
Expressions that update a map
The record epxressions Expr#Name.Field and #Name.Field
Calls to the BIFs specified in tables Type Test BIFs and Other BIFs Allowed in Guard Expressions
Term comparisons
Arithmetic expressions
Boolean expressions
Short-circuit expressions (andalso/orelse)

类型测试 BIF:

    is_atom/1
    is_binary/1
    is_bitstring/1
    is_boolean/1
    is_float/1
    is_function/1
    is_function/2
    is_integer/1
    is_list/1
    is_map/1
    is_number/1
    is_pid/1
    is_port/1
    is_record/2
    is_record/3
    is_reference/1
    is_tuple/1

请注意,大多数类型测试 BIF 都有较旧的等价物,没有 is_ 前缀。保留这些旧 BIF 是为了向后兼容 仅在新代码中不使用。他们也只允许在 顶层。例如,它们在布尔表达式中是不允许的 警卫。

保护表达式中允许的其他 BIF:

abs(Number)
bit_size(Bitstring)
byte_size(Bitstring)
element(N, Tuple)
float(Term)
hd(List)
length(List)
map_get(Key, Map)
map_size(Map)
node()
node(Pid|Ref|Port)
round(Number)
self()
size(Tuple|Bitstring)
tl(List)
trunc(Number)
tuple_size(Tuple)

如果算术表达式、布尔表达式、短路 表达式,或对保护 BIF 的调用失败(因为无效 论点),整个守卫失败。如果守卫是守卫的一部分 序列,序列中的下一个守卫(即跟随的守卫 计算下一个分号)。

这个:

保护序列是由分号 (;) 分隔的保护序列。

给你这个:

GuardSequence = Guard1;Guard2;Guard3

什么是Guard?这句话:

守卫是一系列守卫表达式,以逗号 (,) 分隔。

给你这个:

Guard1 = GuardExpr1, GuardExpr2, GuardExpr3

guard expression 是什么?这句话:

有效保护表达式的集合是...

为您提供以下内容:

GuardExpr1 = (X==1)
GuardExpr2 = (Y==2)
GuardExpr3  = is_atom(Z)

现在,将 GuardExpr 代入这一行:

 Guard1 = GuardExpr1, GuardExpr2, GuardExpr3

给你:

 Guard1 =   X==1, Y==2, is_atom(Z)

现在,将Guard1 替换为这一行:

 GuardSequence = Guard1;Guard2;Guard3

给你:

 GuardSequence = X==1, Y==2, is_atom(Z)

如果你还有一个 Guard2 和 Guard3,它们看起来像这样:

 Guard2 =  is_integer(X)
 Guard3 =  element(Y, Tuple)

那么 GuardSequence 将如下所示:

GuardSequence = X==1, Y==2, is_atom(Z); is_integer(X); element(Y, Tuple)

好的??!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    相关资源
    最近更新 更多