opening it up with Common Lisp

Favorite weblogs

Lisp Related

Bill Clementson

Finding Lisp

Lemonodor

Lispmeister.com

Planet Lisp

Politics

Orcinus

Talking Points Memo

This Modern World

Working for Change

Other home

Polliblog

Recent Readings

Book review: Darwinia
Reviewed: Friday, August 11, 2006

Summer reading: Spin
Reviewed: Saturday, August 5, 2006

Runner
Reviewed: Tuesday, July 18, 2006

the Omnivoire's Delimma
Reviewed: Wednesday, July 12, 2006

the Golem's Eye
Reviewed: Wednesday, May 31, 2006





tinderbox
 width=

The ties that Bind
Saturday, June 19, 2004

Two of Common Lisp's interesting features are destructuring-bind and multiple-value-bind. The first lets you easily pull a list apart using the same syntax that lambda lists use. The second makes it easy to use the multiple values returned by a Lisp function. The trouble with this is that:

  • They are too long to type and read (auto-completion helps here of course);
  • Unlike let, they only work on one list/function at a time. If you have several lists you want to destructure, your code is forced way off to the right -- this is particularly problematic in a presidential election year;
  • They don't work quite the same way. For example, you can use nil in destructuring-bind to indicate that you don't care about a certain variable. This won't work with multiple-value-bind. Instead, you need to put in a variable and then (declare (ignore it));
  • Finally, let, let*, destructuring-bind and multiple-value-bind are all really doing the same thing -- letting you attach convenient temporary names to things so that you can use them more effectively.

Bind is a macro that unifies the four Common Lisp forms. It lets you write code like:

(bind (;; destructuring
       ((name &key x y to-x to-y) (ensure-list slot-spec))
       ;; regular let
       (index (or primary-key key))
       ;; multiple-value-bind
       ((values next-x next-y) (step-path x y to-x to-y)))
  ...)

rather than

rather than:

(destructuring-bind (name &key x y to-x to-y)
                    (ensure-list slot-spec)
  (let ((index (or primary-key key)))
    (multiple-value-bind (next-x next-y)
                         (step-path x y to-x to-y)
      ...)))

bind continued

To my eye, the first is easier to read, more uniform, simpler to write and has less "syntax" than the second. The only thing that the current implementation of bind loses is the difference between let and let*. On the other hand, I've never understood why this difference exists. (Sure it might make it a little easier for the compiler to optimize something (maybe) but even in this case, a little analysis would make it clear whether or not to set the values sequentially or in parallel. I'd be happy to learn that I'm wrong -- or, at least, happy to learn! -- but I think the let / let* distinction is something that we could live easily without.

If you'd like to try bind, you can find on CLiki.net. If you have any questions or comments, let me know.


Home | About | Quotes | Recent | Archives

Copyright -- Gary Warren King, 2004 - 2006