पाठ 25 / 25

Revision and Interview Questions

Recall Haskell concepts quickly for exams and interviews.

Cheat sheet

Core ideas: purity, laziness (call-by-need, thunks, WHNF), strong static typing with inference, GHC, Haskell 2010, GHC2021. Tools: GHCup, GHC, GHCi (:t, :i, :r), cabal, Stack, HLS, Hackage, Stackage, Hoogle. Basics: type signatures, curried functions, partial application, guards, where, let, if, operators and sections, div/mod, fromIntegral. Lists: [] and :, ranges, comprehensions, map, filter, foldr, foldl', zip, partial head. Patterns: equations, _, as-patterns, case, -Wall exhaustiveness. Types: data (sum and product), records and updates, newtype, type, deriving. Classes: Eq, Ord, Show, Num, Semigroup, Monoid, constraints, instances, default methods, orphans. Maybe/Either and do notation. Functor (fmap, <$>, laws), Applicative (pure, <*>, liftA2, traverse), Monad (>>=, do, State, Reader). IO: actions, readMaybe, exceptions (try, throwIO, bracket, withFile). Laziness: infinite lists, space leaks, seq, $!, bang patterns, strict fields. Foldable/Traversable, foldMap, Sum/Max. Real world: modules and exports, Text, Data.Map.Strict, mtl and ReaderT, STM (TVar, atomically, retry), async. Ecosystem: Hspec, QuickCheck, megaparsec, aeson, Scotty, Servant. Advanced: phantom types, DataKinds, GADTs, type families. Performance: -O2, profiling, HLint, Fourmolu.

Common interview questions

Answer each with a short code example.

1. What do purity and referential transparency mean, and why do they matter?
2. Explain lazy evaluation. What is a thunk, and what is a space leak?
3. What is the difference between data, newtype and type?
4. What are type classes, and how do they differ from OOP interfaces?
5. Explain Functor, Applicative and Monad with Maybe as the example.
6. What does do notation desugar into?
7. Why is foldl' usually preferred over foldl?
8. How does Haskell handle I/O while staying pure?
9. Maybe vs Either vs exceptions: when do you use each?
10. What is STM, and why does purity make it practical?
11. What is property-based testing, and how does shrinking help?
12. Name three partial functions and their safe alternatives.

Explain with types

In Haskell interviews, writing the type signature first and reasoning from it ("this takes a function a -> f b and a list, and returns f [b]...") shows exactly the kind of thinking the language rewards.

त्वरित जाँच: Which class provides >>= for sequencing dependent computations?

  • Functor
  • Semigroup
  • Show
  • Monad
Answer

Monad — Monad's bind passes each result to the next computation.