How to match multiple atoms in Erlang?

How to do this, for example

A = atom_a,  
case A of  
 atom_b or atom_c ->   
      %do something here;  
 atom a ->  
      %do something else!  
end.  
+5
source share
2 answers

Try the following:

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.
+8
source

You can use protective devices:

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

All Articles