O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The Either data type has two data constructors and is defined as a sum type as follows:

    data Either a b = Left a | Right b

In our recipe, we used Either String Int, where the right value is of the Int type and the left value is of the String type. In many practical examples, the Left value is used as an error value and the Right value is used as an intended result value. 

In the function safeDiv, we stored the error value in Left as a String and stored the result in the Right value. 

Like Maybe, Either appears in many libraries on package and is a popular choice to represent result values with error, if any.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required