Tuesday, August 19, 2008

Emacs and Ocaml


Now that I am becoming a "real" programmer, I have made the decision to learn how to use the behemoth of a text editor, Emacs. A feature-rich and highly customizable editor, Emacs is distributed by the GNU project and has historically been one of the most popular text editors in programming history, along with vi. Indeed, there has been something of an "Editor War" between the proponents of Emacs and vi for the last twenty years or so, which lead to much flaming on threads, quasi-religious cult movements, etcetera. This was expounded by Randall Munroe in his recent xkcd cartoon, shown above.

Learning Emacs is like learning how to drive stick. It requires a significant investment of time and an understanding of how things work on a more fundamental level, but in the end I believe it is rewarding since you have a much greater control over the machine that you are using. However, instead of having just four or five gears, it is possible to draw on over twenty years of customization by computer programmers around the world that have enabled optimization and automation of nearly every task a programmer needs to do. Effortlessly, I can invoke shell commands, yank snippets of code, use regular expressions to search through files, and edit using multiple windows. I have come to realize that Emacs can, in fact, do everything short of writing the code for me.

Emacs is, however, useless without some good code to input into it. My new mentor, Ashish in the Gerstein lab, uses Ocaml to develop new bioinformatic algorithms, and recommended that I start learning it. I personally had never heard of Ocaml – I expected to learn Perl, Python, perhaps some more Java, since those are the languages popularly used for bioinformatics.

In my one week of using Ocaml, I have become rather attached to it. Like Emacs, it is at first difficult to learn, especially from a conventional Java/C point of view, since it is strongly typed and the syntax is very mathematical. Indeed, it was developed in 1996 by several French mathematicians, and has not achieved great popularity in the US. Yet it has odd ways of being incredibly concise. As an example, here is a Java program that creates a method called “average” that takes the average of two numbers and prints it.

public class Average
{
public static void main(String[] args)
{
System.out.println(average(1,3));
}

public static double average(double a, double b)
{
double avg = (a + b)/2;
return avg;
}
}

Here is the corresponding Ocaml program:

let average x y = (x +. y) /. 2 in average 1 3;;

In addition, Ocaml can reach at least 50% the speed of C programs, which is very good considering that it is also a high level, object-oriented, functional programming language. Also, I can read my code after I write it, which is totally not true for Perl. I'm looking forward to learning it.

I also had a great weekend at Tanglewood, where I heard Jess play an amazing solo for the Enescu octet. I'm also learning some PHP to make a webpage for her new quartet, the Brunell quartet!

1 comments:

Jerry said...

nice... i actually spent all summer doing stuff in emacs, but i never really learned anything about it except for being frustrated at the obsolete buffer files left behind... but then again, i edited only tcl scripts and text tiles.

by the way, i might be switching to computer science engineering, from chemical engineering, this year.