2016-11-08 36 views
1

以下問題的解決方案是否正確?什麼是合法的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) 

回答

3

列表:最大/ 1不是一個有效的組織後衛。只是眼睛球化解決方案,它似乎是正確的,除了列出:最大/ 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)