Skip to content
Tutorials Center
My learning

Topics

Lesson 56 of 57 ยท Practice and interviews

Haskell Interview Questions

Prepare for Haskell interviews with short, accurate answers on laziness, purity, type classes, monads, folds, GHC and tooling, plus runnable stand-ins.

Haskell interviews tend to probe understanding, not trivia. An interviewer wants to hear that you know why purity helps, what laziness costs, and how a monad is just a pattern for chaining. This lesson collects the questions that come up most often, with short answers you can say out loud, and a few follow-ups.

Haskell does not run on this site, so code in the answers is read-only. The two runnable examples are JavaScript stand-ins that illustrate lazy evaluation and monadic chaining.

Language basics

What makes Haskell "purely functional"? Functions have no side effects: the same arguments always give the same result, and nothing is changed behind the scenes. Effects such as printing are described as values of type IO a and performed by the runtime. See purity.

What is referential transparency? You can replace any expression with its value without changing the program's meaning. It enables equational reasoning, safe caching and aggressive compiler optimization.

What is the difference between let and where? Both introduce local bindings. let ... in is an expression and can appear anywhere an expression can. where is attached to a definition and scopes over all its guards. See guards, where and let.

What is currying? Every function takes exactly one argument. add :: Int -> Int -> Int is a function returning a function, so add 1 is a valid partial application. See currying.

What does $ do, and how is it different from .? f $ x is f x with the lowest precedence, handy to avoid parentheses. f . g composes two functions into a new one: (f . g) x = f (g x).

What is the difference between type, newtype and data? type is only an alias. newtype wraps exactly one value with a distinct type and no run-time cost. data defines a brand-new type with any number of constructors and fields. See records and newtype.

Laziness

What is lazy evaluation? An expression is evaluated only when its value is needed, and at most once (call by need). That is why you can define an infinite list and take a finite piece of it.

What are the costs of laziness? Thunks use memory and time, evaluation order is harder to predict, and lazy accumulators cause space leaks. The cure is targeted strictness: foldl', bang patterns, strict fields. See strictness.

What is weak head normal form? A value evaluated far enough that its outermost constructor (or lambda) is known. Just undefined is in weak head normal form; undefined is not. seq forces to this level only.

Why is foldl usually a mistake and foldr sometimes fine on infinite lists? foldl builds a chain of pending operations and cannot stop early. foldr can stop early when the combining function is lazy in its second argument, for example foldr (\x acc -> x > 10 || acc) False [1 ..] returns True after a few elements.

Say it in one sentence

Purity Same input, same output, no hidden effects.
Laziness Compute only what is demanded, once.
Types The compiler checks before you run.
Monad A way to chain steps that carry context.

Short, accurate sentences beat long speeches.

1 / 2

Here is a JavaScript stand-in for laziness. Generators produce values on demand, much like an infinite Haskell list consumed by take:

JS Lazy pipeline (JS stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JavaScript evaluates arguments eagerly, so the stand-in passes functions to delay them. Haskell does this for you, and also caches the result of a thunk after its first use.

Types and type classes

What is a type class? A set of operations a type can support, similar to an interface, but instances are declared separately from types and dispatch happens at compile time through dictionaries. Eq, Ord, Show and Functor are examples. See type classes and custom classes.

What is parametric polymorphism versus ad hoc polymorphism? Parametric: one definition works uniformly for every type (length :: [a] -> Int). Ad hoc: behaviour depends on the type, provided by class instances (show).

What does Eq a => mean in a signature? A constraint: the function works for any type a as long as it has an Eq instance.

What is Maybe for, and how does it differ from null? Maybe a makes absence part of the type, so the compiler forces you to handle Nothing. A null reference can hide in any type and fail at run time. See Maybe and Either.

What is an algebraic data type? A type built from sums (alternatives, as constructors) and products (several fields in one constructor), which you take apart with pattern matching. See algebraic data types.

What is a phantom type? A type parameter that does not appear in any field, used to track facts at compile time, such as Distance Meters versus Distance Miles.

Functors, applicatives and monads

What is a functor? A type constructor f with fmap :: (a -> b) -> f a -> f b that applies a function inside the structure without changing its shape. Laws: fmap id = id and fmap (f . g) = fmap f . fmap g. See functors.

What does Applicative add? pure and <*>, so you can apply a function that is itself inside the context to values in the context. It models independent computations combined together, for example validating several fields. See applicatives.

What is a monad, in practical terms? A type constructor with >>= (bind) and return/pure where the next step may depend on the previous result. In Maybe that means "stop at the first Nothing"; in lists it means "try every combination". See monads.

What is the difference between Applicative and Monad? With an applicative, the structure of the computation is fixed before any result is known. With a monad, a later step can be chosen based on an earlier result, so it is more powerful and less analysable.

How does do notation relate to >>=? It is syntax that the compiler rewrites into >>= and >>. See do-notation.

Is IO special? Its implementation is, but it behaves like any other monad: IO a is a description of an action that yields an a. Only main is run by the runtime. See IO basics.

What are the monad laws? Left identity: return a >>= f equals f a. Right identity: m >>= return equals m. Associativity: (m >>= f) >>= g equals m >>= (\x -> f x >>= g). The compiler does not check them.

This stand-in models the Maybe monad with tagged objects and a bind function:

JS Maybe chaining (JS stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In Haskell >>= is a class method that works for every monad. This stand-in hard-codes one instance.

Monoids, folds and structure

What is a monoid? A type with an associative operation <> and an identity mempty. Lists (++, []), Sum, Product and Ordering are examples. See semigroups and monoids.

What does foldMap do? It maps each element to a monoid and combines the results, so one function covers sums, counts, "any" and "all". It works on any Foldable. See Foldable and Traversable.

What does traverse do? It maps an effectful function over a structure and collects the effects: traverse readMaybeInt ["1","2","x"] gives Nothing because one element failed.

foldr versus foldl'? foldr replaces : with a function and works lazily, good for building lists and short-circuiting. foldl' is a strict left fold, good for sums and counters. See higher-order functions.

GHC and tooling

What are Cabal and Stack? Build tools for Haskell projects and dependencies. Cabal reads a .cabal file and uses Hackage; Stack uses curated Stackage snapshots for reproducibility. See Cabal and Stack.

What does -Wall do and why use it? It enables most useful warnings, including incomplete patterns, unused imports and shadowing. Many teams also use -Werror in CI.

What is a language extension? An opt-in feature enabled by # LANGUAGE ... #, such as LambdaCase or OverloadedStrings. See GHC extensions.

How do you test Haskell code? Unit tests with HUnit or Hspec, and property-based tests with QuickCheck, which generates random inputs and shrinks failures. See testing.

How do you find a performance problem? Compile with -O2, profile with GHC's profiler, look for lazy accumulators and wrong data structures (lists where Data.Map or Vector fit), and benchmark with a library such as criterion. See debugging and profiling.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does call by need mean?

    Call by need is lazy evaluation with sharing: a thunk is evaluated at most once.
  2. 2. Which statement about Applicative versus Monad is correct?

    Monad adds >>=, which lets later steps be chosen from earlier results. Applicative has pure but a fixed structure.
  3. 3. What is the difference between newtype and data?

    type is the alias. newtype creates a new type that is erased at run time and has a single constructor with one field.
  4. 4. Why is foldl' preferred over foldl for sums?

    The result is the same, but the strict accumulator runs in constant space.

Summary

  • Explain purity, referential transparency and laziness in one sentence each, then give an example and a cost.
  • Know the difference between type, newtype and data, and between parametric and ad hoc polymorphism.
  • Functor maps inside a context, Applicative combines independent contexts, Monad chains dependent steps.
  • Monoids and foldMap give you a uniform way to combine data; traverse collects effects.
  • Name the practical tools: -Wall, Cabal or Stack, HUnit, Hspec, QuickCheck and the profiler.

Next: Haskell Cheat Sheet.