Quickrun for incremental haskell/emacs development

I often add quick test functions (like “testme”) inside the haskell module I’m developing. They’re short-lived and soon turn into tests. But I find this approach works better than putting the same lines into ghci because they ‘persist’ for longer.

Since I do this so often, and I use emacs, I automated it. With the following code, I now just hit C-c C-c and emacs loads my module into ghci and runs my test function. Stick a C-u in front, and it’ll let you choose a different function as the default to run.

(add-hook 'haskell-mode-hook 'my-haskell-hook)

(defun my-haskell-hook ()
  (local-set-key "\C-c\C-c" 'haskell-quickrun)

(defvar haskell-quickrun-command "main")

(defun haskell-quickrun (arg)
  (interactive "P")

  (unless (null arg)
	(setq haskell-quickrun-command (read-from-minibuffer "Function to run: ")))

  (inferior-haskell-send-command (inferior-haskell-process) haskell-quickrun-command)
  (end-of-buffer-other-window 0)