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
    name += '.hs' unless name =~ /\.hs$/
    file = name

# 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!

Get Go’in a Little Quicker

Even with Finals literally around the corner I couldn’t help but play around a little with “Google’s New Programming Language” go. In case you haven’t heard about go, you can find out everything you need to know at their simple site

I’m not going to talk about what I like/dislike about the language… yet. Instead I’m going to do what everyone else has probably already done. I wrote a simple compiler/link/run shell script which takes its place in my ~/bin:

# Author: Joseph Pecoraro
# Date: Saturday, November 14, 2009
# Description: Compile, Link, and Run a go script.

# Requires a file
if [ ! "$1" ]; then
  echo "usage: go! <filename>"; exit 1

# Use the proper tools for the arch
if [ $GOARCH = "amd64" ]; then
elif [ $GOARCH = "386" ]; then
elif [ $GOARCH = "arm" ]; then
  echo "unsupported GOARCH: $GOARCH"; exit 2

# Strip .go (if its there)
file=`echo $1 | sed -e 's/.go$//'`

# Compile, Link, and Run (break on any error)
$COMPILER "$file.go" && $LINKER -o $file "$file$EXTENSION" && ./$file

Why did I write my own? The others I had seen were inadequate, didn’t work well with tabbing (filename versus executable name), or didn’t even produce a nice executable name at all. For many, you probably won’t need to compile, link, and run a program all at once (especially in a systems language). However, I’ve found that its a great tool to help me learn the language. This is due in some part to go’s quick compilation and linking.

It is really amazing to me that all of the languages that I have gotten comfortable working with have a command line shell. JavaScript, Haskell, and Ruby all have shells where I can make a fool of myself while quickly testing syntax or libraries. Go doesn’t. Its something I really miss. This little script is a simple replacement that brings me a little closer to interactivity.

I tried to make it generic so it could work on any architecture that go currently builds on. I named the script “go!”. Let me know what scripts you’ve built to work with go.