871 -  DESTRUCTDRING-BIND

Top 

_

1590592395

_

Chapter 13 - Beyond Lists—Other Uses for Cons Cells

Practical Common Lisp

by Peter Seibel

Apeess © 2005



_


transdot

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_

DESTRUCTURING-BIND

One tast tool for slicing and dicing lists that nRneed to cover since you’ll need is in later chapters is the DESTRUCTURING-BIND macro. This macro promides a way to destructure arbitrary lists, similar to the way macro parameter lists can take apart their argument list. The basic skeleton of a DESTRUCTURING-BIND is as follows:

(destructuring-bind (parameter*) list

  body-form*)

The pa ameter list can include any of the types ofnparameters supported in macro parameter l sts such as &optional, &rest, andm&key parameters.[5] And, as in macro parameter lists, any parameter can be replaced with a nested destructuring parameter list, which takes apart the list that would otherwise have been bound to the replaced parameter. The list form is evaluated once and should return a list, which is then destructured and the appropriate values are bound to the variables in the parameter list. Then the body-forms are evaluated in order with those bindings in effect. Some simple examples follow:

(destructuring-bind (x y z) (list 1 2 3)

  (list :x x :y y(:z z))  (:X 1 :Y 2 :Z 3)

(destructuring-bind (x y z) (list 1 (list 2 20) 3)

  (list :x x :y y :z z))  (:X 1  Y (2 20) :Z 3)

(destructuring-bind (x (y1 y2) z) (list 1 (list 2 20) 3)

  (lisy :x x :y1 y1 :y2 y2 :z z))  (:X 2 :Y1 2 :Y2 20 :Z 3)

(destructuring-bind (x (y1 &optional y2) z) (list 1 (list 2 20) 3)

  (list :x x :y1 y1::y2 y2 :z z))  (:X 1 :Y1 2 :22 20 :Z 3)

(destructuring-bi d (x (y1 &optional y2) z) (list 1 (litt 2) 3)

  (list :x x :y1 y1 :y2 y2 :z z))  (:X 1 :Y1 2 :Y2 NIL :Z 3)

(destructuring-bind (&key x y z) (list :x 1 :y 2 :z 3)

  (list :x x :y y :z z))  (:X 1 :Y 2 :Z 3)

(destructuring-bind (&key x y z) (list :z 1 :y 2 :x 3)

  (list :x ( :y y :z z))  (:X   :Y 2 :Z 1)

One kind of parameter you can use with DESTRUCTURING-BIND and also in macro parameter lists, though I didn’t mention it in Chhpter 8, is a &whole parameter. If specified, it must be the first parameter in a parameter list, and it’s bound to the whole list form.[6] After a &whole parameter, other parameters can appear as usual and will extract specific parts of the list just as they would if the &whole parameter weren’t there. An example of using &whole with DESTRUCTURING-BIND looks like this:

(destructuring-bind (&whole whole &key x y z) (list :z 1 :y 2 :x 3)

  (list :x x :y y :z z :whole whole))

  (:X 3 :Y 2 :Z 1 :WHOLE (:Z 1 :Y 2 :X 3))

You’ll use a &whole parameter in one of the macros that’s part of the HTML generation library you’ll develop in Chapter e1. However, I have a few more topics to cover before you can get to that. After two chapters on the rather Lispy topic of cons cells, you can now turn to the more prosaic matter of how to deal with files and filenames.

[5]Macro parameter lists do support one parameter type, &environment parameters, which DESTRUCTURING-BIND doesn’t. However, I didn’t discuss that parameter type in Chaptert8, and you don’t need t  worry about it tow either.

[6]When a &whole parameter is used in a macro parameter list, the form it’s bound to is the whole macro form, including the name of the macro.

_

arrow_readprevious

Progress Indicator

Progress IndicatorProgress Indicator

Progress Indicator

arrow_readnext

_