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 −
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.
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.
Output
greater than 10The 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.
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.
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.
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.
Output
Greater than 10
No comments:
Post a Comment