Page 1 of 1

What exactly does DESTRUCTURING-BIND Do?

Posted: Sun Apr 04, 2010 7:27 pm
by Mercfh
I was reading up on Trees and such in the book "Practical Common Lisp"
but I dont exactly understand what DESTRUCTURING-BIND does exactly? but it seems to be used in alot of tree's when dealing with lisp? can someone give me a very simple layout of what exactly this function does?

(remember its my first time with lisp)

Re: What exactly does DESTRUCTURING-BIND Do?

Posted: Sun Apr 04, 2010 7:53 pm
by nuntius
Here's the CLHS spec.

The basic idea is to bind variables (assign values to them) by performing a simple pattern match (the destructuring part).

The "destructuring lambda list" is very similar to the "lambda list" (argument prototype) used by normal functions or macros. It specifies the pattern to match.

As an example,

Code: Select all

;; normal macro binding
(defmacro f1 (x &key y (z 5))
  (list x y z))
(f1 1 :y 2 :z 3)

;; equivalent destructuring-bind
(destructuring-bind
  (x &key y (z 5))
  '(1 :y 2 :z 3)
  (list x y z))
Sometimes its easier to use destructuring-bind than to write a bunch of code using car and cdr and the like.

Re: What exactly does DESTRUCTURING-BIND Do?

Posted: Sun Apr 04, 2010 8:21 pm
by Mercfh
Oh ok I think I understand, so it's a way of matching.
I've noticed it's used in "Tree's" alot. maybe simply a way of binding values to nodes perhaps