什么是合法的二郎卫兵?

时间:2016-11-08 22:30:26

标签: erlang

波纹管问题的解决方案是否正确?

从下面的列表中选择合法保护表达式,变量A已经绑定。

true, false, apple, 1+2, 1+2 > 3, is_atom(A), B = 3, A = 3, A == 3,length(A), lists:max(A), list_to_atom(A), A and B, (A > 3) and (A < 12)

我的解决方案是(正确的警卫):

true,  false,  1+2 > 3,  is_atom(A), A == 3,  length(A), lists:max(A), list_to_atom(A),  A and B,  (A > 3) and (A < 12)

1 个答案:

答案 0 :(得分:4)

列出:max / 1不是有效的警卫。只是关注你的解决方案,除了那些列表似乎是正确的:max / 1

来自文档(http://erlang.org/doc/reference_manual/expressions.html

  

有效保护表达式集(有时称为保护测试)是有效Erlang表达式集的子集。限制有效表达式集的原因是必须保证对保护表达式的评估没有副作用。有效的保护表达式如下:

The atom true, 
Other constants (terms and bound variables), all regarded as false
Calls to the BIFs specified in table Type Test BIFs
Term comparisons
Arithmetic expressions
Boolean expressions
Short-circuit expressions (andalso/orelse)
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_size(Map)
node()
node(Pid|Ref|Port)
round(Number)
self()
size(Tuple|Bitstring)
tl(List)
trunc(Number)
tuple_size(Tuple)