Introduction to Haskell or
In Haskell we have or operator to compare the values of the variable, this operator also comes under the lexical notation of the Haskell programming language. This operator works in the same way as any other programming language, it just returns true or false based on the input we have provided. Also, we can use any number of or operators there is no such restriction for that. Or operator is represented by using the ‘||’ double pipe symbol in Haskell. Also, it is an in-built operator available in Haskell, we don’t require to include anything to use this while programming. In the section of the tutorial, we will see the internal working and how it behaves in different output, also its implementation and usage in detail.
As we know that it is used to compare the different values given together, on the basis on which it returns true or false. Let’s take a closer look at its syntax which is given by Haskell official documentation for better understanding see below;
expr 1 || expr 2
As you can see in the above line of syntax we are just simply using the ‘||’ or operator to return the result based on the evolution of the input passed. Let’s take a look at the practice syntax for better understanding see below;
1>10 || 2>3
From the above line of practice syntax, it is clear that it is very easy to use and handle. In the coming section, we will see the actual internal working also how it returns true and false based on the input for beginners to understand it better.
How or operator works in Haskell?
As we already know that what or operator does in Haskell, but in general it has several cases which decide what to return a result, we will have closer look at its table to get a better knowledge of this. But now it is clear that it is used to compare the different values based on their result it returns us one single result which is a Boolean value of True or False. First, we will see the Signature for this operator given by the Haskell official documentation see below;
Bool -> Bool -> Bool
2. ‘||’: We use one defined symbol to represent the or operator in Haskell, which is represented by the double pipe ‘||’. This symbol should be present in every condition we have. so on this basis, it will return us the result.
Now we will see the truth table for or operator which is very basic and followed by all the programming language, let’s get started to see below;
The truth table for or operator:
|Condition 1||Condition 2||Result|
As you can see in the above truth table for or operator we are trying to pass some inputs based on this it will return us the result which will be a simple true or false value. Let’s underrated each input steps in detail for better understanding see below;
- If the first expression is returning false and the second expression is also returning false then the generator output will be False from the or operator.
- If the first expression is returning false and the second expression is also returning true then the generator output will be true from the or operator.
- If the first expression is returning true and the second expression is also returning false then the generator output will be true from the or operator.
- If the first expression is returning true and the second expression is also returning true then the generator output will be true from the or operator.
This is how or operates works in general, which is very easy to understand it will follow this table to return anything from the expression l=of series as well while programming.
Let’s take a sample piece of code to understand the or operator in more detail see below;
main = do
print(1>10 || 2>3)
In the above lines of code, we are comparing two expressions using or operator in Haskell. First, we have defined the main function after this we have written our logic we want to execute and compare. Here we have two expressions that are just comparing two numbers using the operator. In the first condition, we are comparing ‘1 > 10 ‘ here the condition will return as false, in the second condition we are comparing ‘2 > 3’ this condition will also return false, so this whole expression evolution will be false by the Or operator, you can run this and check with different values also. This is how it works in general or in Haskell also.
1) In this example we are trying to compare the several expressions together using the or operator in Haskell, and based on this it will return us true or false. This is a sample example for beginners to start using or operator for better comparison see below;
main = do
print("Demo to show or operator in Haskell !!")
print("Below we are using || operator for checking expression")
let result1 = 1>10 || 2>3
let result2 = 10>10 || 5< 10
let result3 = 10<9 || 3> 10
let result4 = 20>10 || 200 > 300
let result5 = 30>60 || 1<=0
let result6 = 40>80 || 9==9
let result7 = 50>10 || 8 > 7
print("printing result here !!")
print("result one is ::" , result1)
print("result two is ::" , result2)
print("result three is ::" , result3)
print("result four is ::" , result4)
print("result five is ::" , result5)
print("result six is ::" , result6)
print("result seven is ::" , result7)
By using or operator we can compare the different expressions and return a common value as the Boolean of true or false based on the condition we have passed, this makes the comparison easy and we can return or check anything based on the operator. We can use this as per the requirement of the logic.
This is a guide to Haskell or. Here we discuss the internal working and how it behaves in different output, also its implementation and usage. You may also have a look at the following articles to learn more –