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
Pingback: Erlang - Pattern Matching | Adglob Infosystem Pvt Ltd