What I cannot create, I do not understand.

Just because you've implemented something, doesn't mean you understand it.

Setting up Emacs for Lisp hacking on OS X, pt. 2: Common Lisp and Clojure

This is likely out of date. You probably don’t want to follow these instructions. This is what I’m using now:


I updated this after using it for a while and finding out that there are problems using the most recent CVS snapshot SLIME with swank-clojure, and then after trying to answer someone’s question about this problem over on StackOverflow

These are the rest of my notes for setting up Lisp hacking on OS X, focusing on using SLIME with Common Lisp and Clojure. I’m using SBCL and Emacs from Homebrew with --cocoa. I have no idea if this will work with other Common Lisp implementations or with Aquamacs.

What seems to be the recommended way to use SLIME with Common Lisp is to use the most recent version from CVS. However, swank-clojure only works with the SLIME package in ELPA (according to this discussion on the swank-clojure Github page). And this version from ELPA is stripped down and won’t work with Common Lisp.

In order to use SLIME with both Common Lisp and Clojure in a single Emacs (and change from one to the other after restarting Emacs, but without fiddling with settings in .emacs) I had to resort to a bit of a hack.

These are the steps I did to get this to work:

With a fresh Emacs (no configuration at all, so move ~/.emacs and ~/.emacs.d somewhere else for the moment) install ELPA:


From within Emacs, install the packages “slime” and “slime-repl”. (M-x package-list-packages then C-s slime then i to select and x to install)

Move the files in ~/.emacs.d/elpa/slime-20100404 and ~/.emacs.d/elpa/slime-repl-20100404 to a new directory like ~/hacking/lisp/elpa-slime.

Throw out the ELPA install: $ rm -rf .emacs.d.

Now clone the emacs-starter-kit and move it to .emacs.d. I only did this with a fresh copy from technomancy’s Github, so try that first if you have problems.

(If you want to use Scheme too, and have followed the instructions in part 1, just save your username.el somewhere and add the two functions below once you get Common Lisp and Clojure working. You should be able to have all three set up in the same Emacs, although not running at the same time)

Get the latest SLIME with CVS:

cvs -d :pserver:anonymous:[email protected]:/project/slime/cvsroot co slime

OS X doesn’t come with CVS, but it’s installed along with XCode and the developer tools from Apple, so it might be installed on your system already.

I moved slime to ~/hacking/lisp/cvs-slime.

Hopefully it’s obvious what the Emacs Lisp below does:

(defun slime-common-lisp ()
          (setq inferior-lisp-program "/usr/local/bin/sbcl") ; your Common Lisp impl
          (add-to-list 'load-path "~/hacking/lisp/cvs-slime/")  ; your SLIME from CVS directory
          (require 'slime)
          (slime-setup '(slime-repl))

          (defun slime-clojure ()
          (add-to-list 'load-path "~/hacking/lisp/elpa-slime")
          (require 'slime)
          (slime-setup '(slime-repl))
          (slime-connect "localhost" 4005))

Now M-x slime-common-lisp will start the Common Lisp runtime and give you a SLIME REPL.

For Clojure you’d have to start the Clojure runtime and swank-clojure on port 4005, Leiningen comes with a utility for this:

$ ~/.lein/bin/swank-clojure

You can also create a new Leiningen project and start a swank-clojure inside that, but if you just want a vanilla REPL to send code to from Emacs, the above works.

Then in Emacs: M-x slime-clojure. You should now have a new buffer with a Clojure REPL with the prompt user>. Remember that you have to restart Emacs if you want to switch from one to the other. If someone knows how to avoid this, please let me know (you’d have to unload SLIME and reload the one that works with the language you want).

Setting up Emacs for Lisp hacking on OS X, pt. 1: Scheme

This weekend I took a bus from NYC to Boston and then on to Portland, ME for the holiday (it’s Thanksgiving weekend here in the US). I find it hard to do any real work on a bus because I tend to get a little car sick (I was quite a puke-y kid growing up) so I decided to start fiddling with my Emacs set up, which wouldn’t take too much deep thought.

I’m going to be posting my notes over the next few days; they might be useful for someone else. I’ve benefited greatly from people posting howtos like this on random blogs, plus when I hose it I can always refer back to this.

Install some implementation of Scheme if you don’t have one installed already. I’ve been using MIT Scheme for SICP, but you can get Gambit Scheme, Scheme 48 and PLT Scheme (via mzscheme) through Homebrew. MIT Scheme has a pre-built binary for OS X which includes their Edwin Emacs clone and a command line interpreter.

Vanilla Emacs is also in Homebrew, and probably in Macports and Fink as well. For Homebrew, you’ll probably want to specify --cocoa when installing it to get Emacs.app, which I’d advise you move into the OS X /Applications directory, which will allow you to start Emacs via Spotlight.

I would highly recommend checking out Phil Hagelberg’s “Emacs Starter Kit” which includes a huge number of customizations and niceties for getting started with Emacs. The best way to use this is to fork the project on Github, create your own branch (or not), clone it on your machine and then move the directory to ~/.emacs.d, being careful if you already had some Emacs stuff in there you want to keep.

He provides a way of adding your own customizations by creating a ~/.emacs.d/username.el where username is replaced by your system username. Now you can add all your own stuff in there, or add stuff to override things you don’t want from the Emacs Starter Kit (personally I don’t like the faded parens, visual bell, and pretty lambdas, but that’s all largely personal preference). Now you can keep your changes separate and pull updates the the starter kit in the future, without messing everything up.

To get a Scheme REPL inside of Emacs, you need to configure Emacs to use the particular Scheme binary of our implementation. Since I’m using MIT Scheme, this means /Applications/mit-scheme.app/Contents/Resources/mit-scheme. If you are using something else, use the path to that binary instead. I tried it with mzscheme from Homebrew and it worked, but I don’t know about the others.

Add this to your ~/.emacs.d/username.el, replacing string with the path to your Scheme binary:

(setq scheme-program-name 

MIT Scheme has an extended interface for interacting with the REPL called “xscheme”, which includes things like M-o for evaluating an entire buffer. This should be included with Emacs. (If it’s not, you can get it here )

Add this to your ~/.emacs.d/username.el file to use this interface:

(defun load-xscheme () (require 'xscheme)) 
(add-hook 'scheme-mode-hook 'load-xscheme)

Now M-x run-scheme will open your chosen Scheme implementation in a new buffer and let you send the expression before the cursor with C-x C-e.

If the extra xscheme commands don’t work, or if Emacs throws up when you start it, maybe there is some issue with the xscheme.el library. Try downloading it separately, saving it into the ~/.emacs.d directory and adding this to your ~/.emacs.d/username.el file:

(load "~/.emacs.d/xscheme.el")

Restart Emacs and try the xscheme commands again.

I’ll add part two, for setting up Common Lisp and Clojure using SLIME, sometime soon.

The Wizard Book

I just finished watching the the first lecture from the Structure and Interpretation of Computer Programs chapter on streams (chapter 3, section 5). I felt compelled to post and once again highlight my naivete when I wrote about Haskell’s laziness compared to Scheme, because the lecture (and the chapter in the text, I’m sure. I’m about to sit down for the reading.) discusses the implementation of lazily evaluated streams of data.

SICP is a fascinating book. It’s so good, it makes me want to press it into freshman CS students’ and fellow Blub programmers’ hands. I know MIT has moved their introductory course to robotics and Python, and I won’t attempt to judge them for it, but the book and lectures are still incredibly engaging. I wish I had found it sooner, when I was still a student, but I guess it’s better late than never. Maybe the reason I’m so enthralled with it is exactly because I went through a rather mundane, Java-centric curriculum. Anyway, if anyone is reading this and has been on the fence about reading SICP, here’s another Internet soul urging you to just go start. You have no excuse: it’s free!

The Road to Lisp

I haven’t posted in over a month mostly because I’ve been spending a lot of my evenings studying Lisp. Althought the first post here was on Haskell, it actually would have been more appropriate had it been on Lisp, because Lisp (Scheme) was a largely responsible for me rediscovering the joy in programming. So I’ve been thinking about writing a Lisp post for a while, something like a “Road to Lisp” response. But instead of a lengthy post I’ll just get to the point and actually respond to the survey. Thus…

I, (spacemanaki), do solemnly offer my responses to “The Road to Lisp Survey”:

When did you first try Lisp seriously, and which Lisp family member was it?

In the winter of 2009 I read The Little Schemer and wrote all of the exercise programs out on paper in Scheme. Later I actually installed MIT Scheme and played around in the REPL.

Many of us had multiple run-ins with Lisp before it “stuck”. The “stick” date is of most interest, but you can share earlier encounters if you like.

I had an early encounter with Scheme in college. But we spent less than a month with it (needless to say they weren’t teaching a variation on 6.001 at the Big State Java School) and I didn’t appreciate it as much as I did after The Little Schemer. At the time I was curious, but somehow got distracted. All I took away was cons, car, cdr, and tail-call optimization. I didn’t loathe its syntax though, as many programmers do. Maybe that alone was a sign of things to come.

What led you to try Lisp?

I watched some iteration of Douglas Crockford’s talk based on his book JavaScript: the Good Parts. He put up a slide with code similar to this:

[code]var digit_name = function () { var names = [‘zero’, ‘one’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’, ‘seven’, ‘eight’, ‘nine’]; return function(n) { return names[n]; }; }(); alert(digit_name(3));[/code]

And he said that if there was only one thing the audience should take away from the talk, it was this. I paused the video and had to take a long look at the code, because I had never seen anything like it. The first sentence in the Wikipedia article on closures) is a gem: “In computer science, a closure is a first-class function with free variables that are bound in the lexical environment.” This was mostly incomprehensible to me.

On Crockford’s site there is a review of the Little Schemer, and I picked it up with a desire to better understand what he meant that JavaScript was more like Scheme than like Java or that it was “Lisp in C’s clothing.” After reading it and its sequel, The Seasoned Schemer, I not only understood closures, but was completely delighted by Scheme.

What other languages have you been using most?

I’m paid to write Java and C at my day job, and occasionally do some simple web projects using JavaScript (with HTML/CSS). I’ve been staying up late learning Haskell and Ruby.

How far have you gotten in your study of Lisp?\ I know this is hard to quantify. Just wing it.

I’ve read the aforementioned Little Schemer and its sequel. I’ve since moved on to the Structure and Interpretation of Computer Programs; I’ve only read two (small) fifths of that, but I’m far from giving up yet, it’s just slow going. Most recently I’ve started to learn Common Lisp from Paul Graham’s ANSI Common Lisp and Peter Seibel’s Practical Common Lisp. I would say I have only just begun my study.

What do you think of Lisp so far?

I think Lisp is very compelling. I think it’s delightful and incredibly fun. I don’t feel frustrated by the language, instead I feel empowered and challenged.

postscript to Android with Vim: Eclim is awesome!

I have to admit that after working on some simple Android apps using Vim and building on the command line, I realized to my dismay that I was missing some of the bloat from Eclipse, specifically the automatic handling of import statements.

I could just use Eclipse, but it’s too slow for me. If text input becomes even at all sluggish because the compiler is running in the background, it’s too slow. I’m not the only one who thinks this: Brad Fitzpatrick said the same thing in Coders at Work. Some people might just say “get a more powerful development machine,” but I don’t think that’s really fair. It might solve the problem, since I’ve been working on a netbook (I’m about to upgrade to a Macbook Pro, for all the same old reasons, despite new mixed feelings about Apple). It still doesn’t solve the problem that I want to use Vim and not Eclipse. So I started looking around for other people who hack Java in Vim. I quickly stumbled upon Eclim, which is a brilliant tool.

Basically it runs Eclipse in a server-mode and provides Vim with code completion, validation, import handling and more through a bunch of Vimscripts. What’s really nice is that Vim is still pretty fast, in part probably because it doesn’t actually do the continuous compiling all the time, but instead only compiles and validates when you save a file. I struggled a little bit to get it to find the Android libraries, but eventually it worked. (I hadn’t pointed the ADT plugin to the SDK directory in Eclipse, but I think it would be fine if your Eclipse is already properly configured first.)

Android development with … Vim?

It was enormously refreshing to find this while I was setting up the Android SDK and developer tools recently:


For the past several months I’ve been working on our BlackBerry application at work. Despite my very limited experience with Android (I’m really only at the “Hello, World.” level) I think that based on the link above, I’d be safe to assume that the support and documentation from Google is much better than what RIM offers.

RIM has their own custom IDE for BlackBerry development (written in Swing so it’s pretty clunky). There’s also an official Eclipse plugin, but the most recent release doesn’t support older versions of the BlackBerry OS, making testing on a range of devices tricky (or impossible). I’ve mostly been using Netbeans and its generic J2ME/mobile plugin, because that’s what my co-worker suggested. It works, but just barely: you can install older component packages to test your app on older phones, which is worth putting up with the flaky debugger connection (and you should be testing on a phone anyway).

You actually can use any editor for BlackBerry development, and build your app using ant, bb-ant-tools, and antenna. In fact this is how we do automated builds for testing and releases, but you could easily develop this way too. I admit that I’m picking on RIM and BlackBerry a little bit, but this really is just the tip of my grievances with the platform. I don’t think I am alone.

Anyway, finding semi-official support for other editors and other OSes (BlackBerry development is completely Windows-based) was quite a pleasant surprise. While I’m sure that the Android Eclipse plugin is great, my muscle memory is heavily invested in Vim’s command set and modal editing style. And my preferred development environment is the flavor du jour of GNU/Linux (currently Ubuntu seems to be the strongest contender, but more on that later perhaps).

I’ll be spending some more time with the Android SDK in the coming weeks, so I’ll be able to follow up about my initial impressions and confirm if I was correct.

The Little Schemer

I just finished re-reading The Little Schemer by Daniel Friedman and Matthias Felleisen, this time working through the exercises in a Scheme interpreter (mostly mit-scheme but occasionally I opened up the DrScheme debugger, which was quite useful for exploring continuations and for bouncing around in the interpreter from the final chapter). It is one of the most delightful books I have ever read, programming related or otherwise, so I’d be remiss not to share my thoughts here.

The tone is very light-hearted, and while the Socratic question-and-answer style is a little strange at first, I think in the end it works quite well. Serious programmers should not cast it aside because it is so didactic or even because it starts out easy if you’ve studied recursion even a little bit. The last three or four chapters are what you are paying for.

The authors do not hold back here, and present such ambitious and interesting topics as the halting problem, Church numerals, a derivation of the Y combinator, continuations, and in the final chapter build a simple Scheme interpreter. There is plenty here to captivate any programmer with a little curiosity or anyone else who might be interested in why computing is a worthy subject.

This book is part of what drove my renewed interest in Scheme, Lisp and functional programming and really in Computer Science and programming in general, and lead me to pick up its sequel, The Seasoned Schemer, as well as start reading The Structure and Interpretation of Computer Programs (more on that later). I wish I had read it earlier, but in some ways maybe I found it at just the right moment, when I was in need of being reinvigorated.

I owe Douglas Crockford for finding this, after reading the glowing review on his site I was intrigued and bought it sort of on a whim. I haven’t read the third book in the series yet, but it’s definitely high on my list, as is the authors’ book on ML. I will probably re-read The Seasoned Schemer again soon, and spend some more time gushing about it then.

Graham’s scan, Graham Hutton, Wikipedia, and CLRS

The author’s of RWH point to the Wikipedia articles on convex hulls and Graham’s scan as a reference for the exercise I mentioned last week. Wikipedia’s great, but Introduction to Algorithms (CLRS) has a much clearer presentation of the algorithm I think (Wikipedia has pseudo-code from Algorithms by Sedgewick and Wayne but I haven’t actually read that so I won’t presume to judge it based on someone’s “adaptation”). It’s tempting to edit the article with the version from CLRS, only to have it reverted a few days later, but I think I will refrain.

Finding a convex hull from a set of points is a nice problem because convex hulls can be described so easily in two dimensions by visualizing a rubber band around nails driven into a board. The Graham scan is also quite easily understood, at least at the abstract level. I found this great animation which the user David Ashley uploaded and posted to the Discussion page which really illuminates how the algorithm works:


I’m not going to belabor this too much, and I certainly won’t do a code review of my Haskell solution, that would be too precocious, if you’re really interested it’s on Github.

Completing this exercise coincided with the delivery from Amazon of Graham Hutton’s Programming in Haskell, which is very nice, exactly what \ it claims to be: a simple, clear, and concise introduction to the language. I’m already several chapters into it, my only real criticism is the strange choice to use certain symbols instead of actual Haskell syntax (such as an arrow instead of minus-greater-than) but that’s easily overlooked, or maybe not, depending on your taste.

laziness, Stack Overflow

I had said that learning Haskell would be an interesting diversion from Scheme (it has been), and mentioned Haskell’s lazy evaluation. When I wrote that I meant in contrast to Scheme’s strict evaluation, because I thought that was the strategy that Scheme uses, which is true: that’s the default in Scheme. However while browsing the Haskell tag on Stack Overflow, I came across a comment which lead me to search for “Scheme lazy evaluation” and to reading about delaying evaluation with the “delay” keyword, as well as creating a thunk by passing a function literal which will evaluate the expression you want to delay when it is called.

Here’s a clear example from StackOverflow:


Speaking of SO, here’s a really tremendous answer to one of the many “how to I learn language X” in regards to Haskell:


Such a trove of links and resources. While browsing SO I also discovered that one of the author’s of Real World Haskell is really active on the site, which is awesome.


Read the comments

It’s kind of weird that the online edition of Real World Haskell has a comments section for every paragraph. There are some interesting things discussed in the threads attached certain exercises and examples, and occasionally the authors have chimed in, but for the most part, it’s just a lot of chatter and distraction. I thought it was a little strange in the PHP manual, and that only has a comments section for every page. (Albeit there is probably more noise in the PHP manual comments than RWH… zing!)

Actually, there is a lot of whining about discussing advanced topics too soon in the RWH comments, which I think is silly. After all, it’s not http://learnyouahaskell.com/. Anyway for the most part I find them distracting because if I don’t immediately understand a sentence or paragraph, I might open the comments and end up feeling more confused, instead of just re-reading the section, backing up and re-reading what preceded it, or moving on and finding answers later in the page. It turns reading into a much more fragmentary experience. I guess what I really should do is just get the print edition.

If the comments distracted you as much as they did me, here’s a tip:

wget –mirror -p –convert-links -P ./rwh http://book.realworldhaskell.org/read/

Otherwise though it’s really great, and I’ve only read the first three chapters. There are some really cool exercises, even though it’s early in the book, such as implementing the Graham scan algorithm for finding a convex hull from a set of points.


This seems like a kind of “not-so-real-world” problem, although Wikipedia does list a few practical uses I didn’t know about. It’s still a neat problem and it’s interesting to work on it in Haskell. I’ll probably start pushing my solutions (at least to the more interesting problems) to my Github.