Subject: Re: understanding destructuring-bind From: Erik Naggum <erik@naggum.net> Date: Wed, 27 Feb 2002 14:55:26 GMT Newsgroups: comp.lang.lisp Message-ID: <3223810531213034@naggum.net> * Jordan Katz <katz@underlevel.net> | I've read both Graham's explanation and the Hyperspec's of | destructuring-bind, but it still makes no sense. : | Can someone explain? Let me start with the simplest example there is, a single variable: (destructuring-bind (<variable>) <list> <body>) destructuring-bind hacks up <list> suck that the first (and probably only) element in that list becomes the value of <variable> in <body>, almost exactly like (let ((<variable> (car <list>))) <body>) Now, suppose you make this a tad more complex, with a list of variables and a list of values: (destructuring-bind (<var1> <var2> <var3>) <list> <body>) (let ((<var1> (car <list>)) (<var2> (cadr <list>)) (<var3> (caddr <list>))) <body>) Finally, let us try with this "tree" notion, not just with a list of variables: (destructuring-bind (<var1> <var2> <var3> (<var4> <var5>)) <list> <body>) (let ((<var1> (car <list>)) (<var2> (cadr <list>)) (<var3> (caddr <list>)) (<var4> (car (cadddr <list)>)) (<var5> (cadr (cadddr <list)>))) <body>) So far, I have only mimicked the results of macroexpand on the destructuring-bind form, but to macroexpand is intimately tied with the function of destructuring-bind. There is another construct that uses destructuring-bind in Common Lisp and that is macros, so if we do (defmacro foo (<var1> <var2> <var3> (<var4> <var5>)) <body>) we get exactly the same kind of destructuring when you evaluate the form with a <list> equal to (1 2 3 (4 5)) as when you invoke (foo 1 2 3 (4 5)). I hope this made it somewhat clearer what this binding form does and why it is available to you as a Common Lisp programmer. /// -- In a fight against something, the fight has value, victory has none. In a fight for something, the fight is a loss, victory merely relief.