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)
Tags: Lisp
December 16, 2007 at 5:44 am
I’ve read before that Norvig “hates those unsightly #’ marks” needed for most lambda calls. He defined a macro for that, too:
(defmacro lambda (args &body body)
“Allow (lambda (x) …) instead of #’(lambda (x) …)”
‘#’(lambda ,args .,body))
This is quoted from his paper at:
http://norvig.com/self-eval.pdf
I still don’t know how I feel about the reader macro version you presented. It seems like it would come in handy in a lot of places where you only need one argument (which, admittedly, is what lambdas turn out to be most of the time). I’d like to see something, though, for the general n-argument case. I’ll need to give that more thought.
December 16, 2007 at 5:47 am
Or better uses of compose and curry.
December 16, 2007 at 11:20 am
The lambda macro made it into the ANSI spec: http://www.lisp.org/HyperSpec/Body/mac_lambda.html
I’ve seen 2 proposals for the n-argument case.
1. number the args: (+ _1 _2)
2. just drop the lambda: ((a b) (+ a b))
I don’t know how to feel about #L either, I thought I’d use it a lot because it really bugs me when I have to break a line just to use a closure, but I’ve found it doesn’t happen very often. I’m also not yet convinced that it doesn’t hurt readability, but I’ll give it a bit more of a trial before making any final decisions.
December 17, 2007 at 5:03 am
If it made it into the standard, why does everybody write
#’(lambda (x) (+ 2 x))?
^ (why this?)
December 17, 2007 at 5:18 am
[...] I would like to present some higher-order functions in different representations and analyse the pros and cons of using each type of form. There are some macros used below. I’ve stored the code for them. This post was inspired by (code) vs ‘(data). [...]
December 17, 2007 at 7:10 am
Eric: they’re obviously writing substandard code. :-)
December 17, 2007 at 8:53 pm
Well, using if using #’ is so horrible why don’t you program in a lisp-1 :). Reading the Eric Norman tag-back, his POV seems to be if I see an HOF idiom that reminds me I’m in a lisp-2 I won’t use it.
As far as extending #L, see also http://srfi.schemers.org/srfi-26/srfi-26.html
December 17, 2007 at 11:23 pm
Eric Normand: sorry I misspelled your name.
Everyone else: sorry for the grammatical errors.
I wrote a quick port of srfi-26 in common lisp here: http://paste.lisp.org/display/52700
December 18, 2007 at 3:48 am
@eric:
(lambda (…) …) -> #’(lambda (…) …) didn’t make it into the standard to save people the trouble of typing sharp-quote, but rather to facilitate implementing ISLISP in common lisp. (take a look at footnote 13 in chapter 5 of siebel’s practical common lisp for further details). while omitting the leading #’ is shorter, lisp-2 fans tend to find it inconsistent with the way named functions are passed as arguments.