« September 2007 | Main | April 2008 »

March 2008 Archives

March 4, 2008

stealing from arc

Paul Graham's Arc language has been causing a commotion in the Lisp and web application communities. One of the stand-out features of Arc is its tidy syntax for simple anonymous functions. In Arc, the increment function can be expressed thus, using square brackets:

[+ 1 _]
Compare this to Common Lisp's completely readable, but less-than-fun, syntax for the same function:
(lambda (x) (+ x 1))

Arc's implementation of the bracket syntax is in brackets.scm, written by Eli Barzilay. Using Eli's Scheme code as a guide, I wrote the following Common Lisp reader macro:

(defun square-bracket-reader (stream char)
  (declare (ignore char))
  `(lambda (&optional _ __)
    (declare (ignorable _ __)) ; don't warn about unused variables
    ,(read-delimited-list #\] stream t)))

(defun use-square-bracket-readtable ()
  ; Install #'square-bracket-reader as a reader macro for [
  (set-macro-character #\[ #'square-bracket-reader)
  
  ; Make ] behave like ) to the reader so that [+ 1 _] works,
  ; not just [+ 1 _ ].
  (set-macro-character #\] (get-macro-character #\) nil)))

Usage is very simple:

CL-USER> (use-square-bracket-readtable)
T
CL-USER> ([+ 1 _] 10)
11
CL-USER> ([+ _ __] 1 2)
3

About March 2008

This page contains all entries posted to Brad Ediger in March 2008. They are listed from oldest to newest.

September 2007 is the previous archive.

April 2008 is the next archive.

Many more can be found on the main index page or by looking through the archives.