Archive for November, 2007

Lambda Shortcut

November 29, 2007

I recently reread the Tutorial on Good Lisp Programming Style by Peter Norvig and Kent Pitman. One thing that surprised me was the suggestion that it “may be appropriate sometimes” to use read macros to shorten lambda expressions:

(reduce #'+ numbers :key #L(if (oddp _) (* _ _) 0))

I’ve always avoided using that idiom because it’s non standard, but seeing it in such a highly regarded style guide is enough for me to add it to my bag of utilities. Here’s my definition:

(set-dispatch-macro-character   #\# #\L #'(lambda (stream sub-character infix-parameter)
             (when infix-parameter
               (error "#~a does not take an integer infix parameter."
                      sub-character))
             `#'(lambda (,(intern "_"))                   ,(read stream t nil t))))  (I'd like to know how to prevent wordpress from mangling my code)
Advertisements