Книга: Practical Common Lisp
DESTRUCTURING-BIND
DESTRUCTURING-BIND
One last tool for slicing and dicing lists that I need to cover since you'll need it in later chapters is the DESTRUCTURING-BIND
macro. This macro provides 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 parameter list can include any of the types of parameters supported in macro parameter lists such as &optional
, &rest
, and &key
parameters.[149] 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)
(list :x x :y1 y1 :y2 y2 :z z)) ==> (:X 1 :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 :Y2 20 :Z 3)
(destructuring-bind (x (y1 &optional y2) z) (list 1 (list 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 x :y y :z z)) ==> (:X 3 :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 Chapter 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.[150] 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 31. 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.
- Data Binding Using the GridView Control
- Providing DNS for a Real Domain with BIND
- Using Fedora's BIND Configuration Tool
- 15.7.1 Назначение rpcbind
- 2.4.3. Dynamic Binding
- 10.3.3. Binding a Client to a Server
- The BIND 8 host.conf File
- Browsing and Manipulating Bindery Data
- Configuring server bindings
- Data Binding
- Главный конфигурационный файл BIND
- Запуск сервера BIND в рамках поддерева chroot