Idiomatic Functional Programming
Examples of Idiomatic FP
Here are example codebases we will spend time inspecting and critiqueing.
Minesweeper
- Minesweeper at Exercism.io
- Its not the full game, just calculating the number of mines adjacent to each non-mine square
- Example input/input:
[ " * "; " * "; "*****"; " * "; " * "; ] [ " 2*2 "; "25*52"; "*****"; "25*52"; " 2*2 "; ]See test.ml for more examples
- Before getting into the code let’s consider algorithms, there are two distinct approaches
- For each non-mine square look around it and add up the mines
- Or, for each mine square increment the count on all non-mine squares around it (start at 0)
- The latter is fundamentally more difficult to do functionally
- The grid will need to change (mutate) a great many times as counts bump up one by one
- The former can produce the complete answer for a given cell in one go, no incrementalism
- We will review this functional solution
- We made another variation on the functional version to be cleaner and more efficient
- Will look at an imperative approach which has some poor abstractions and fails to use combinators.
- All of these minesweeper versions are in this zip file
- Before getting into the code let’s consider algorithms, there are two distinct approaches
Other Examples
- ocaml-cuid is a utility to generate highly random string IDs for webpages etc.
- Lots of nice piping here plus use of functors to build Unix and JavaScript variations
- dolog is a very simple logging utility
- Shows some nice use of state,
include, and aMakefunctor.
- Shows some nice use of state,
- We may also look at a past homework solution so you can compare it with what you did.