Erlang 定义保护序列和什么是有效的保护表达式?

Erlang 定义保护序列和什么是有效的保护表达式?,erlang,expression,guard,Erlang,Expression,Guard,我想知道保护序列以及Erlang中的有效保护表达式是什么? 保护序列是由分号(;)分隔的保护序列。 如果至少有一个防护为真,则防护序列为真。(修订) 剩余的防护装置(如果有)不进行评估。) 担保1;。。。;GuardK 例如: go(X,Y) when X==1;Y==2 -> io:format("Yes~n"). go(X,Y,Z) when X==1,Y==2, is_atom(Z) -> io:format("Yes~n"

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

保护序列是由分号(;)分隔的保护序列。 如果至少有一个防护为真,则防护序列为真。(修订) 剩余的防护装置(如果有)不进行评估。)

担保1;。。。;GuardK

例如:

go(X,Y) when X==1;Y==2 ->
   io:format("Yes~n").
go(X,Y,Z) when X==1,Y==2, is_atom(Z) ->
    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)
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
因此,分号类似于

保护是一系列保护表达式,由逗号(,)分隔。 如果所有保护表达式的计算结果都为true,则保护为true

GuardExpr1,…,GuardExprN

例如:

go(X,Y) when X==1;Y==2 ->
   io:format("Yes~n").
go(X,Y,Z) when X==1,Y==2, is_atom(Z) ->
    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)
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
因此,逗号类似于

有效保护表达式集是有效保护表达式集的子集 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)
型式试验BIFs:

    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
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都有较旧的等价物,没有 是前缀。保留这些旧BIF是为了向后兼容 仅和不用于新代码中。他们也只允许在 顶级的。例如,在中的布尔表达式中不允许使用它们 警卫

保护表达式中允许的其他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
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
Guard1 = GuardExpr1, GuardExpr2, GuardExpr3
什么是
防护装置
?这句话:

保护是一系列保护表达式,由逗号(,)分隔

给你这个:

GuardSequence = Guard1;Guard2;Guard3
Guard1 = GuardExpr1, GuardExpr2, GuardExpr3
什么是
保护表达式
?这句话:

有效的保护表达式集为

给你一些东西,比如:

GuardExpr1 = (X==1)
GuardExpr2 = (Y==2)
GuardExpr3  = is_atom(Z)
现在,将GuardExpr替换为以下产品线:

 Guard1 = GuardExpr1, GuardExpr2, GuardExpr3
 GuardSequence = Guard1;Guard2;Guard3
给你:

 Guard1 =   X==1, Y==2, is_atom(Z)
 GuardSequence = X==1, Y==2, is_atom(Z)
现在,将
Guard1
替换为此行:

 Guard1 = GuardExpr1, GuardExpr2, GuardExpr3
 GuardSequence = Guard1;Guard2;Guard3
给你:

 Guard1 =   X==1, Y==2, is_atom(Z)
 GuardSequence = X==1, Y==2, is_atom(Z)
如果您还有一个Guard2和Guard3,它们看起来是这样的:

 Guard2 =  is_integer(X)
 Guard3 =  element(Y, Tuple)
GuardSequence = X==1, Y==2, is_atom(Z); is_integer(X); element(Y, Tuple)
那么GuardSequence将如下所示:

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

好吧

欢迎来到堆栈溢出。这些问题似乎有点太宽泛了。文件上怎么说?你哪里有问题?