Erlang – Guards

Erlang Guards

In this guide, we will discuss Erlang Guards. Guards are constructs that we can use to increase the power of pattern matching. Using guards, we can perform simple tests and comparisons on the variables in a pattern.

The general syntax of the guard statement is as follows βˆ’

function(parameter) when condition ->

Where,

  • Function(parameter) βˆ’ This is the function declaration that is used in the guard condition.
  • Parameter βˆ’ Generally the guard condition is based on the parameter.
  • Condition βˆ’ The condition which should be evaluated to see if the function should be executed or not.
  • The when statement must be used when a guard condition is specified.

Let’s look at a quick example of how guards can be used βˆ’

Example

-module(helloworld). 
-export([display/1,start/0]). 

display(N) when N > 10 ->   
   io:fwrite("greater then 10"); 
display(N) when N < 10 -> io:fwrite("Less 
   than 10"). 

start() -> 
   display(11).

The following things need to be noted about the above example βˆ’

  • The display function is defined along with a guard. The first display declaration has a guard of when the parameter N is greater than 10. So if the parameter is greater than 10, that function will be called.
  • The display function is defined again, but this time with the guard of less than 10. In this way, you can define the same function multiple times, each with a separate guard condition.

The output of the above program will be as follows βˆ’

Output

greater than 10

The guard conditions can also be used for if else and case statements. Let’s see how we can carry out the guard operations on these statements.

Guards for β€˜if’ Statements

Guards can also be used for if statements so that the series of statements executed is based on the guard condition. Let’s see how we can achieve this.

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   N = 9, 
   if 
      N > 10 -> 
         io:fwrite("N is greater than 10"); 
      true -> 
         io:fwrite("N is less than 10") 
   end.

The following things need to be noted about the above example βˆ’

  • The guard function is used along with the if statement. If the guard function evaluates to true, then the statement β€œN is greater than 10” is displayed.
  • If the guard function evaluates to false, then the statement β€œN is less than 10” is displayed.

The output of the above program will be as follows βˆ’

Output

N is less than 10

Guards for β€˜case’ Statements

Guards can also be used for case statements so that the series of statements executed is based on the guard condition. Let’s see how we can achieve this.

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   A = 9, 
   case A of {A} when A>10 -> 
      io:fwrite("The value of A is greater than 10"); _ -> 
      io:fwrite("The value of A is less than 10") 
   end.

The following things need to be noted about the above example βˆ’

  • The guard function is used along with the case statement. If the guard function evaluates to true, then the statement β€œThe value of A is greater than 10” is displayed.
  • If the guard function evaluates to anything else, then the statement β€œThe value of A is less than 10” is displayed.

The output of the above program will be as follows βˆ’

Output

The value of A is less than 10

Multiple Guard Conditions

Multiple guard conditions can also be specified for a function. The general syntax of the guard statement with multiple guard conditions is given below βˆ’

function(parameter) when condition1 , condition1 , .. conditionN ->

Where,

  • Function(parameter) βˆ’ This is the function declaration that used the guard condition.
  • Parameter βˆ’ Generally the guard condition is based on the parameter.
  • condition1, condition1, .. conditionN βˆ’ These are the multiple guard conditions which are applied to functions.
  • The when statement must be used when a guard condition is specified.

Let’s look at a quick example of how multiple guards can be used βˆ’

Example

-module(helloworld). 
-export([display/1,start/0]). 

display(N) when N > 10 , is_integer(N) -> 
   io:fwrite("greater then 10"); 
display(N) when N < 10 -> 
   io:fwrite("Less than 10"). 
   
start() -> 
   display(11).

The following point needs to be noted about the above example βˆ’

  • You will notice that for the first display function declaration, in addition to the condition for N>10, the condition for is_integer is also specified. So only if the value of N is an integer and greater than 10, this function will be executed.

The output of the above program will be as follows βˆ’

Output

Greater than 10

Next Topic : Click Here

This Post Has One Comment

Leave a Reply