The Full Wiki

More info on Erlang Programming/guards

Erlang Programming/guards: Wikis

Advertisements

Note: Many of our articles have direct quotes from sources you can cite, within the Wikipedia article! This article doesn't yet, but we're working on it! See more info or our list of citable articles.

Wikibooks

Up to date as of January 23, 2010

From Wikibooks, the open-content textbooks collection

< Erlang Programming

Erlang Guards

Guard structures

Legal guards in Erlang are boolean functions placed after the key word, "when" and before the arrow, "->". Guards may appear as part of a function definition, 'receive' 'if', 'case' and 'try/catch' expressions.

We can use a guard in a function definition
Example program: guardian.erl

-module(guardian).
-compile(export_all).
   
the_answer_is(N) when N == 42 -> true;
the_answer_is(N) -> false.
   
% ============================================= >% 
%
% Example output:
%
% c(guardian).
% ok
%
% guardian:the_answer_is(42).
% true
%
% guardian:the_answer_is(21).
% false

and Fun definition

 F = fun(N) when N == 42 -> true;
              (N) -> false
        end.

receive expression

 receive
   {answer, N} when N == 42 -> true;
   {answer, N} -> false
 end.

if expression

 if 
   N == 42 -> true;
   true -> false
 end.

case expression

 case L of
   {answer, N} when N == 42 -> true;
   _ -> false
 end.

and try/catch

 try find(L) of
    {answer, N} when N == 42 -> true;
    _ -> false
 catch
    {notanumber, R} when is_list(R) -> alist;
    {notanumber, R} when is_float(R) -> afloat
    _ -> noidea
 end.

You will notice that in these examples it would be clearer to remove the guard and modify the pattern matching instead.

Literate programming note: Anonymous match variables that start with an underscore like "_" are not generally recommended. Rather, it is nice to use some descriptive variable name like "_AnyNode". On the otherhand, for tutorial code like this, a descriptive variable is more distracting than helpful.

 case L of
   {node, N} when N == 42 -> true;
   _AnyNode -> false
 end.
Advertisements

Guard functions

There are several built-in-functions (BIFs) which may be used in a guard. Basically we are limited to checking the type with, is_type(A) and the length of some types with, type_size() or length(L) for a list length.

is_alive/0                     
is_boolean/1                    
is_builtin/3                    
is_constant/1                   
is_float/1                      
is_function/2      is_function( Z, Arity)               
is_function/1                  
is_integer/1                    
is_list/1                       
is_number/1                     
is_pid/1                        
is_port/1                      
is_record/3                    
is_record/2                     
is_reference/1                 
is_tuple/1      

tuple_size/1
is_binary/1
is_bitstring/1
bit_size/1
byte_size/1        
length(Z) > N 
A > B
A < B
A == B
A =< B
A >= B
A /= B
A =:= B     exactly equal
A =/= B     exactly not equal

Note: all erlang data types have a natural sort order.

atom < reference < port < pid < tuple < list ...

Guards can be compound expressions.

the_answer_is(N) when N == 42, is_integer(N) -> true;

Advertisements






Got something to say? Make a comment.
Your name
Your email address
Message