Updated April 7, 2023
Introduction to Haskell either
In Haskell either is used to represent the possibility of two values. Either is used to represent two values that can be correct or error. It has two constructors also which are named Left and Right. These constructors also represent and show some purpose either in Haskell. Here Left constructor will show the error value, while on the other hand Right constructor will show the correct value. In the coming section of the tutorial, we will see how this function works in Haskell in more detail, and also we will see its implementation and usage while doing programming in Haskell.
Syntax
In this section we will see the syntax for either in Haskell, which is given by their official documentation, let’s have a closer look at the syntax of it for better understanding see below;
data Either a b
As you can see in the above line of syntax we have its basic definition with us. Here we are using either keyword followed by two values ‘a’ and ‘b’. Let’s have a look at the practice syntax for either, which will be more helpful to get a better idea for either in Haskell for beginners to see below;
Example:
let variable_name = Your_constructor "value if any" :: Either a basic
How does either work in Haskell?
As we already know that either is used to represent two values which can be either correct or error. It also contains two constructors which further used to represent these values in Haskell either. So by the use of either, we can represent our value which is either correct or error. In this section we will first see its definition by the Haskell documentation to get a better idea, let’s get started to see below;
Method signature:
data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)
As you can see in the above line of definition for either in Haskell, is given by the Haskell official documentation. This is available inside the Data library, so we can use it as Data.eithe in Haskell. Also if we can see this closely we have two values here ‘a’ and ‘b’. These values are represented by the Left and Right constructor which will turn represent the value as correct and error. After the ‘=’ operator in the above definition, we are trying to represent the ‘a’ value by the Left constructor, and ‘b’ values is represented by the Right constructor. Below we will see the detailed description of both these constructors for better understanding see below;
Available Constructor:
1. Left: As per the official documentation of the Haskell, and as per the convention Left constructor in either is used to represent the error value. Below see the syntax to use it for better understanding see below;
Example:
Left a
1. Right: As per the official documentation of Haskell, and as per the convention Right constructor in either is used to represent the correct value. Below see the syntax to use it for better understanding see below;
Example:
Right a
Now we will see some of the methods which can be used with either to perform desired logic while programming see below;
1. isRight: This method is used to check whether we have passed the right value or not. It will return us the Boolean true or false. If the value if Right value then it will return true else it will return false. Below see the definition of the method as per the Haskell documentation for better understanding see below;
Example:
isRight :: Either a b -> Bool
Now we will see one practice syntax how we can use this while programming in Haskell;
Example:
isRight (Right "your value ")
As you can see in the above line of code we are using the isRight method followed by the Right value passed inside the bracket. In this way, you can use it to evaluate the result of the passed value.
2. isLeft: This method is used to check whether we have passed the left value or not. It will return us the Boolean true or false. If the value is Left value then it will return true else it will return false. Below see the definition of the method as per the Haskell documentation for better understanding see below;
Example:
isLeft :: Either a b -> Bool
Now we will see one practice syntax how we can use this while programming in Haskell;
Example:
isLeft (Left "your value ")
As you can see in the above line of code we are using the isLeft method followed by the Left value passed inside the bracket. In this way, you can use it to evaluate the result of the passed value.
Examples
1. In this example we are trying to identify the value as string or int by using the either Left and Right constructor available. Remember if you want to use either while programming does not forget to import this in your program otherwise it will not work.
Example:
import Data.Either
main = do
print("Demo to show wokring of either in Haskell !!")
print("Using either to evaluate the value either instance of string or inte using either in Haskell !!")
let val1 = Left "Hello" :: Either String Int
let val2 = Right 100 :: Either String Int
let val3 = Left "Hellow world" :: Either String Int
let val4 = Left "I m string !!" :: Either String Int
let val5 = Right 900 :: Either String Int
let val6 = Left "I am another string" :: Either String Int
let val7 = Right 500 :: Either String Int
print("Printing the result after either evalution !!")
print("result one is :: ", val1)
print("result two is :: ", val2)
print("result three is :: ", val3)
print("result four is :: ", val4)
print("result five is :: ", val5)
print("result six is :: ", val6)
print("result seven is :: ", val7)
Output:
Conclusion
By the use of either in Haskell we can easily identify the passing value is correct or error. We also have so many methods available which can be used either to get the result fast for passing Right and Left value in Haskell. Mainly it is used to show the possibility between the two values either a or b.
Recommended Articles
We hope that this EDUCBA information on “Haskell either” was beneficial to you. You can view EDUCBA’s recommended articles for more information.