我如何匹配Erlang中的多个原子?

时间:2012-03-17 18:56:36

标签: erlang match

我如何进行以下操作,例如

A = atom_a,  
case A of  
 atom_b or atom_c ->   
      %do something here;  
 atom a ->  
      %do something else!  
end.  

2 个答案:

答案 0 :(得分:9)

你可以使用警卫:

A = 'atom_a',
case A of
  B when B =:= 'atom_b'; B =:= 'atom_c' ->   
    %do something here;  
  'atom_a' ->  
    %do something else!  
end.  

答案 1 :(得分:8)

尝试以下方法:

case is_special_atom(A) of
    true ->
        %do something here;
    false ->
         %do something else!
end.

is_special_atom(atom_b) -> true;
is_special_atom(atom_c) -> true;
is_special_atom(_) -> false.