Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I match multiple atoms in Erlang?

Tags:

match

erlang

How do I do the below, for example

A = atom_a,  
case A of  
 atom_b or atom_c ->   
      %do something here;  
 atom a ->  
      %do something else!  
end.  
like image 432
n_emoo Avatar asked Mar 17 '12 18:03

n_emoo


2 Answers

You can use guards:

A = 'atom_a',
case A of
  B when B =:= 'atom_b'; B =:= 'atom_c' ->   
    %do something here;  
  'atom_a' ->  
    %do something else!  
end.  
like image 146
aronisstav Avatar answered Nov 03 '22 08:11

aronisstav


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.
like image 11
sch Avatar answered Nov 03 '22 08:11

sch