What exactly does DESTRUCTURING-BIND Do?

Discussion of Common Lisp
Post Reply
Mercfh
Posts: 17
Joined: Tue Mar 30, 2010 3:39 pm
Location: Miami,FL

What exactly does DESTRUCTURING-BIND Do?

Post by Mercfh » Sun Apr 04, 2010 7:27 pm

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)

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: What exactly does DESTRUCTURING-BIND Do?

Post by nuntius » Sun Apr 04, 2010 7:53 pm

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.

Mercfh
Posts: 17
Joined: Tue Mar 30, 2010 3:39 pm
Location: Miami,FL

Re: What exactly does DESTRUCTURING-BIND Do?

Post by Mercfh » Sun Apr 04, 2010 8:21 pm

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

Post Reply