« The Cover | Main | A Modest Syntax Proposal: RBXML »

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

Comments (1)

Of course, _ is just another symbol name in CL. Neat!

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on March 4, 2008 11:41 PM.

The previous post in this blog was The Cover.

The next post in this blog is A Modest Syntax Proposal: RBXML.

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