English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Erlang Guard

Guard can be used to add pattern matching functionality. With Guard, we can perform simple tests and comparisons on variables in the pattern.

The general syntax of the Guard statement is as follows-

function(parameter) when condition ->

Where

  • Function(parameter) This is the function declaration used under protection conditions.

  • Parameter −通常,保护条件基于参数。

  • Condition −Evaluate the condition to determine whether the function should be executed.

  • When a guard condition is specified, the when statement must be used.

Let's see a simple example of how to use guards-

Instance

-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)

For the above examples, the following points should be noted:

  • The display function is defined with a guard. When the parameter n is greater than10When, the first display declaration has a guard. Therefore, if the parameter is greater than10,the function will be called.

  • The display function is defined again, but this time the protection is less than10。In this way, you can define the same function multiple times, each with a separate Guard condition.

The output of the above program is as follows:

greater than 10

Protection conditions can also be used with if else and case statements. Let's see how to perform protection operations on these statements.

Guard of the if statement

Guards can also be used with if statements so that a series of statements to be executed are based on protection conditions. Let's see how to do this.

-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.

For the above examples, the following points should be noted:

  • The guard function is used with the if statement. If the calculation result of the guard function is true, it will display the statement "N is greater than10”。

  • If the calculation result of the guard function is false, it will display the statement "N is less than10”。

The output of the above program is as follows:

Output

N is less than 10

Guard of the "case" statement

Protection can also be used with case statements so that a series of statements to be executed are based on protection conditions. Let's see how to achieve this goal.

Instance

-module(helloworld). 
-export([start/0]). 
start() -> 
   A = 9, 
   case A of {A} when A>10 -> 
      io:fwrite("The value of A is greater than10"); _ -> 
      io:fwrite("The value of A is less than10") 
   end.

For the above examples, the following points should be noted:

  • The guard function is used with the case statement. If the calculation result of the guard function is true, it will display the statement "The value of A is greater than10”。

  • If the calculation result of the guard function is another value, it will display the statement "The value of A is less than10”。

The output of the above program is as follows:

Output

The value of A is less than10

Multiple Guard Conditions

Multiple protection conditions can also be specified for a function. The general syntax for a protection statement with multiple protection conditions is as follows-

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

Where

  • Function(parameter) −This is a function declaration using protection conditions.

  • Parameter −通常,保护条件基于参数。

  • condition1, condition1, .. conditionN −These are multiple guard conditions applied to functions.

  • When a guard condition is specified, the when statement must be used.

Let's see how to use a simple example with multiple guards-

Instance

-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)

Regarding the above examples, the following points should be noted:

  • You will notice that, for the first display function declaration, in addition to N>10condition, in addition, the condition is specified as is\u integer. Therefore, only when the value of N is an integer and greater than10the function will be executed.

The output of the above program is as follows:

Output

Less than 10