WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Haskell · Intermediate · question 31 of 100

Explain how to do error handling in Haskell with the Either type.?

📕 Buy this interview preparation book: 100 Haskell questions & answers — PDF + EPUB for $5

Error handling in Haskell is typically done using the ‘Either‘ type. The ‘Either‘ type is defined as a sum type with two type constructors, ‘Left‘ and ‘Right‘:

data Either a b = Left a | Right b

Here, ‘a‘ represents the type of the error value and ‘b‘ represents the type of the success value. By convention, we use ‘Left‘ to represent errors and ‘Right‘ to represent success.

To better illustrate error handling using ‘Either‘, let’s take the following example: we want to implement a division function which can handle division by zero errors. First, we need to define an error type:

data DivisionError = DivisionByZero
  deriving (Show)

Now, we can write the safe division function using the ‘Either‘ type:

safeDiv :: Double -> Double -> Either DivisionError Double
safeDiv _ 0 = Left DivisionByZero
safeDiv x y = Right (x / y)

To handle errors propagated by ‘Either‘, we can use the ‘case‘ expressions or the ‘do‘ notation when chaining multiple computations.

For example, let’s write a function that computes the reciprocal of a double:

reciprocal :: Double -> Either DivisionError Double
reciprocal x = safeDiv 1 x

Now, let’s write a function that takes two doubles, computes their reciprocals, and then adds the results:

addReciprocals :: Double -> Double -> Either DivisionError Double
addReciprocals x y = do
  rx <- reciprocal x
  ry <- reciprocal y
  return (rx + ry)

The ‘do‘ notation automatically propagates errors through the computation. If an error occurs, the rest of the block will be skipped, and the error value will be returned. As a result, when adding reciprocals, we won’t encounter any division by zero errors.

Finally, to use the ‘addReciprocals‘ function, we can pattern match on the result using a ‘case‘ expression:

main :: IO ()
main = do
  let x = 2
  let y = 0
  case addReciprocals x y of
    Left DivisionByZero -> putStrLn "Error: Division by zero occurred!"
    Right result        -> putStrLn $ "The sum of the reciprocals is: " ++ show result

In this example, the result will be an error because ‘y‘ is 0, and the error message will be printed.

To sum up, error handling in Haskell with the ‘Either‘ type involves:

1. Defining an error type for the specific type of error

2. Using the ‘Either‘ type to express error or success values in functions

3. Handling errors by pattern matching with ‘case‘ expressions or using ‘do‘ notation to chain computations

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Haskell interview — then scores it.
📞 Practice Haskell — free 15 min
📕 Buy this interview preparation book: 100 Haskell questions & answers — PDF + EPUB for $5

All 100 Haskell questions · All topics