Classic Computer Science Problems in Haskell

May 22, 2026 · View on GitHub

This repository contains my attempt at porting the source code of the books Classic Computer Science Problems in Python and Classic Computer Science Problems in Java by David Kopec (see Classic Computer Science Problems) to Haskell.

The source code is organized by chapter, treating each chapter as a package and code meant to be 'run' as an executable within that package. The executable name roughly matches the name of the example in the Python book, e.g. fib1, fib2 etc. as used in chapters 1 of the books. Note that the source code file names start with an uppercase letter, though. So the source code of the fib1 one example is in a file named Fib1.hs

If you installed Haskell using GHCup, thus have cabal installed, you can compile and run the examples in a terminal by cd-ing into the corresponding chapter folder and executing cabal run <name of the example>, for example cd chapter1 cabal run fib2.
You can also run the examples using the interactive Haskell interpreter. To do so, cd into the corresponding chapter folder and start cabal repl <example>, e.g. cabal repl Fib2. Execute the example by calling the main function after the example is loaded into the interactive interpreter. You can also try the other functions defined in the example, of course.

If the example has no build dependency other than base (see line build-depends: of the corresponding example in the chapter<n>.cabal file), you can also simply start ghci and load the example using :load <Example>, e.g. :load Fib2 (remember that the file names start with an uppercase character), or start ghci in the root folder and load the example using :load chapter<n>/<Example>. In any case, you can execute the example by calling the main function after the example is loaded into the interactive interpreter.

Code meant for reuse such as GenericSearch of chapter 2 and CSP of chapter 3 is treated as a library.

Done:

  • Chapter 1:
    • Fibonacci series, examples fib1, fib2, fib3, fib4, fib5, fib6
      ❗️fib4 does not compile with the recent GHC versions (GHC >= 9.8.1) because the package memoize it uses does not compile with the recent GHCs❗️ It works with GHC 9.4.8, though.
    • Trivial (gene) compression: trivialCompression
    • Unbreakable Encryption: unbreakableEncryption
    • Calculating pi: calculatePi
    • The towers of Hanoi: hanoi
  • Chapter 2:
    • DNA search: dnaSearch
    • Generic search (library - run cabal test to execute the simple examples from the book as unit tests)
    • Finding a path through a Maze: maze
    • Missionaries and cannibals problem: missionaries
  • Chapter 3
    • Constraint Satisfaction Problem (CSP) framework (library)
    • Coloring a map example: mapColoring
    • 8 Queens problem: queens
    • Word puzzle: wordSearch
    • SEND + MORE = MONEY example: sendMoreMoney
  • Chapter 4
    • Graph framework (library)
    • Find the shortest path - run cabal test to execute the example as unit test of the graph framework.
    • Minimize cost / minimum spanning tree: mst
    • Find shortest path with weighted graph: dijkstra
  • Chapter 5
    • General genetic algorithm (library)
    • Simple equation as simple test: simpleEquation
    • SEND + MORE = MONEY example: sendMoreMoney
    • ListCompression
  • Chapter 6
    • General KMeans algorithm (library)
    • Governors example: governors
    • (Michael Jackson) Albums example: albums
  • Chapter 7
    • Simple neural networks framework (library)
    • Classification with classical Iris data: iris
    • Classification with wine data example: wine
  • Chapter 8
    • Adverarial search for board games (library)
    • Tic Tac Toe example: tictactoe (play against an "AI")
    • Connect Four example: connectFour (play against an "AI")
  • Chapter 9
    • The Knapsack problem: knapsack
    • Traveling salesman: tsp
    • Phone number mnemonics: phoneNumberMnemonics