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

Haskell · Basic · question 7 of 100

What is a monad in Haskell?

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

A monad is an abstraction in Haskell that provides a way to chain and sequence computations with values that may be of a particular context, like computations that can fail, or functions that perform side effects (such as input/output). Monads are structures that implement the ‘Monad‘ typeclass which dictates two primary operations: ‘return‘ (also called ‘pure‘ for Applicative instances) and ‘bind‘ (also called ‘>>=‘), along with a few associated laws.

In Haskell, the ‘Monad‘ typeclass is defined as:

class Applicative m => Monad m where
    return :: a -> m a       -- sometimes called pure for Applicative instances
    (>>=)  :: m a -> (a -> m b) -> m b  -- bind function, which is the essence of the monad

Intuitively, a monad can be thought of as a container or a context that encapsulates values. Let’s look at each operation in more detail:

1. ‘return‘ / ‘pure‘: This function takes a value of type ‘a‘ and wraps it in a monadic context of type ‘m a‘. It serves as the starting point for building monadic computations as it wraps a "plain" value into a monadic context.

2. ‘bind‘ (also represented as ‘>>=‘): The bind function allows you to chain monadic computations. It takes an instance of the monad ‘m a‘, and a function ‘a -> m b‘ that takes a value of type ‘a‘ and returns a value in the monadic context ‘m b‘. ‘bind‘ is responsible for applying the function to the value and returning the result in the monadic context.

Here’s an example using the ‘Maybe‘ monad:

data Maybe a = Nothing | Just a

instance Monad Maybe where
    return x = Just x

    Nothing >>= _ = Nothing
    (Just x) >>= f = f x

In this example, the ‘Maybe‘ monad represents a computation that might have failed and returned a ‘Nothing‘, or succeeded and returned a ‘Just‘ value. The purpose of the bind function in this case is to chain computations that can potentially fail, bypassing any further computations if the value is ‘Nothing‘.

Let’s consider a simple example:

divide :: Float -> Float -> Maybe Float
divide _ 0 = Nothing
divide x y = Just (x / y)

example :: Maybe Float
example = do
    x <- divide 12 4
    y <- divide 20 x
    return y

Here, we use the ‘Maybe‘ monad to handle the case where dividing by 0 would result in a runtime error. Instead, using the ‘Maybe‘ monad, we can chain the computations and safely handle the failure case. The ‘example‘ value will be ‘Just 1.666...’, or if any of the divisions are by 0, it would be ‘Nothing‘.

In conclusion, a monad in Haskell is a powerful and flexible abstraction that allows you to encapsulate and sequence computations in a context while maintaining a clean and expressive syntax. Monads are useful in various cases, such as handling failures, dealing with side effects, working with asynchronous code, or managing state, among others.

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