I’ve been reading a book written by Chris Allen on Haskell - Haskell Programming from First Principles. This is a fantastic book that I would recommend to anyone interested in functional programming. More and more I am learning to appreciate how wonderful functional programming can be. Strict typing makes a lot of simple bugs evaporate. The module structure of Haskell is also simple. Typeclasses are a far more elegant way of specifying behaviour than the convolutions of Java’s interfaces (at least, I find them more intuitive). If object oriented programming is frustrating you, give Haskell a try.
The game
I built snake in Haskell. It total, it was 247 lines of code, so relatively straightforward. The repo lives here - clone it and enjoy.
Basically, one thread runs continually listening for input. Another thread runs a game loop, repeating every 0.3s (although you can modulate this parameter for a faster or a slower game). The Snake.hs
module contains all of the functions that operate on the snake (these are pure functions - so no side effects). All side effects are contained within the Main.hs
file. This was fun to build and surprisingly fun to play, as banal as snake may seem.