Simple TDD workflow for small Haskell projects

Alexandru Rosianu
AR.Blog();
Published in
2 min readDec 11, 2017

I’m working on my second coursework for the COMP2209 Programming 3 module at University of Southampton, which is all about functional programming principles and Haskell. For this assignment we’re given 5 standalone exercises related to Lambda Calculus and Combinatory Logic; I assume this is the base onto which we’ll be writing our own programming language in the second semester.

Having promised a friend that next time I work on an assignment I’ll use TDD, I had to live up to it, so I spent some time getting my workflow right. I also took this chance to learn more about Haskell’s ecosystem: build systems, modules, packages, test frameworks, and so on.

The first thing I tried was stack, a tool that integrates compiling, building, testing and benchmarking in one command. It’s quite powerful and I’d love to use it on a serious project one day, but for my coursework I wanted something simpler than what stack new offers. So I was only going to use stack to install and run GHC.

As for the test framework, I found Hspec, which did the job wonderfully. Of course, I installed it with stack. My project structure on disk was just two files: Challenges.hs and Spec.hs.

Setup

mkdir cw2 && cd cw2            # create project folder
stack setup # ensure GHC is installed
stack install hspec # install the test runner
touch Challenges.hs Spec.hs # create project files

Running

After writing some code, I wanted to run the tests:

stack runhaskell Spec.hs

And that’s all I need, since for this assignment we only need to submit one .hs file with our functions—we don’t need to compile or turn it into an executable.

Productivity boost

I’m used from the node.js world to set up flows that automatically rebuild when files change on disk—I wanted the same for my Haskell project. entr(1) is an awesome little tool that does just that. With the following command, entr will rerun tests every time I change one of my *.hs files:

ls *.hs | entr stack runhaskell Spec.hs

Stepping up the game

I opened both files + a terminal running the above command in nvim. Now I can write Haskell and see the test results at the same time.

Full screen nvim.

Happy haskelling!

--

--