Posts Tagged ‘Lisp’

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

Practical Common Lisp

September 27, 2007

So I set up a blog and then promptly forgot about it, not a good start. I’ll endeavour to post once a month from now. Anyway, gigamonkey was asking for some link love for his book which was the real impetus behind this post.

Common Lisp tutorial
Lisp tutorial

According to Donald Knuth, Peter is now destined to write many more books, so I’ll take this opportunity to say ‘More Practical Common Lisp’ is a catchy title.