Lambda Shortcut

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)
About these ads

Tags:

10 Responses to “Lambda Shortcut”

  1. Eric Normand Says:

    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.

  2. Eric Normand Says:

    Or better uses of compose and curry.

  3. rottcodd Says:

    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.

  4. Eric Normand Says:

    If it made it into the standard, why does everybody write

    #’(lambda (x) (+ 2 x))?
    ^ (why this?)

  5. Toolchest: Shortcuts for higher-order functions | LispCast Says:

    [...] 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). [...]

  6. ken Says:

    Eric: they’re obviously writing substandard code. :-)

  7. Matthew Swank Says:

    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

  8. Matthew Swank Says:

    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

  9. justin Says:

    @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.

  10. How far can LISP macros go? « « Programmers Goodies Programmers Goodies Says:

    [...] this page for how to do [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: