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 (2)
Of course, _ is just another symbol name in CL. Neat!
Posted by John Connors | March 5, 2008 6:03 AM
Posted on March 5, 2008 06:03
This uses the keywords library in mzscheme:
(require (lib "kw.ss"))
(define-macro (fx . exp) ; not "body" -- use "begin" to extend
`(lambda/kw (#:optional $1 $2 $3 $4 $5 $6 $7 $8 $9)
,exp))
The comment is a note-to-self that if you want more than one expression in your fuction, then you should use "begin".
"nine arguments ought to be enough for anybody"
--rocky
Posted by Anonymous | October 26, 2008 2:51 PM
Posted on October 26, 2008 14:51