I’d like to share a little repo/exercise I build to help new commers. It consists of building the snake game in Haskell. Nothing fancy: no ncurses or frontend, just spit out a bytestring to the console in the shape of a snake board.
The challenge aims to teach monads refactoring your code, so you build the application twice, first using a pure implementation with all plain regular functions (arguebly, using some state-monad like functions). Then you refactor so the state pattern becomes very clear. Then you refactor again so the reader pattern emerges, etcetera… Eventually you define your own set of
MonadXXX defining exactly the interface you want.
Another important aspect is that it doesn’t try to be a tutorial at all. It is a challenge. The student is given a Haskell file with
undefined function and an explanation of what the function should do. It is up to the student to recollect all the resources and to read the documentation. Refactors are explained in
I think this has two main benefits:
- The learner escapes from tutorial hell. The skeleton is given to you, but you have to do your own research
- By writing the application twice I think you stop fearing the monad (at least, Reader and State) as they emerge as sort of “good practise” more than an obscure abstract concept.
By good practise I mean that using
State actually lead to much better code quality (readability and expressiveness) than useng their “pure” counterparts as they are pretty much just pure functions with different composition (well, any monad actually, but very noticeble in these two). So I think this challenge makes a good work explaining the why instead of the what
You can find it here