This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

What exactly does DESTRUCTURING-BIND Do?

3 posts · 1035 views

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?

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,
;; 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?

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