Removing Haskell’s Hassle

This goes along with my recent post about my go! script for go. I wrote a similar script for Haskell to overcome a problem I continually had with Haskell’s interpreter.

The problem is that in order to create useful functions to test in the interpreter (ghc) you need to load your file into the interpreter. However, by the time I’ve opened up the interpreter I forgot to open up an editor (via the shell). Still, if I had a document open than linking the two takes a bit of work (mostly drag and drop). I’ve managed to spearhead the issue when I first start the interpreter.

My little script allows me to quick edit a file, or even start a new file, and automatically link up with the interpreter. Its another one of the scripts you can find in my ~/bin:

#!/usr/bin/env ruby
# Author: Joseph Pecoraro
# Date: Wednesday, September 30, 2009
# Description: Simple Haskell Quick Starter

# Defaults
EDITOR = 'mate' # ENV['EDITOR'] for most Unix Guys
HASK_DIR = File.expand_path('~/haskell')

# Determine the File
#  - default to temp.hs
#  - use provided file if exists
#  - create new temp file with given name
file = File.join(HASK_DIR, 'temp.hs');
if ARGV.size > 0
  name = ARGV[0]
  if File.file? name
    file = name
  else
    name += '.hs' unless name =~ /\.hs$/
    file = name
  end
end

# Work the magic
system "touch #{file}"
system "#{EDITOR} #{file}"
system "ghci #{file}"

Its a nearly perfect replacement of ghci for me. It allows me to focus more on the task at hand rather then getting bogged down and frustrated when I realize I have to stop what I’m doing to link the interpreter to an editor.

For your own use you may want to customize the EDITOR and HASK_DIR variables. As I tend to use TextMate and a folder in my home directory for throw away files (which I can easily clean up eventually). Let me know if you’ve suffered with the same problem, or others when working with Haskell!

Add a Comment

search