1 ;; This is a typical usage, for pulling apart a list 2 (destructuring-bind 3 (first second) 4 '(1 2) 5 (format t "~%~%;;; => first:~a second:~a~&" first second)) 6 ;;; > first:1 second:2 7 8 ;; You can also pull apart improper lists: 9 (destructuring-bind 10 (first . second) 11 '(1 . 2) 12 (format t "~%~%;;; => first:~a second:~a~&" first second)) 13 14 ;;; > first:1 second:2 15 16 ;; The first argument to destructuring-bind is a lambda list, but you 17 ;; can grab the remainder by either using a dotted list: 18 19 (destructuring-bind 20 (first second . stuff) 21 '(1 2 3 4 5) 22 (format t "~%~%;;; => first:~a second:~a rest:~a~&" first second stuff)) 23 24 ;;; => first:1 second:2 rest:(3 4 5) 25 26 ;; or you can grab the remainder with &rest, just like you do for 27 ;; functions that take a variable number of arguments: 28 (destructuring-bind 29 (first second &rest stuff) 30 '(1 2 3 4 5) 31 (format t "~%~%;;; => first:~a second:~a rest:~a~&" first second stuff)) 32 33 ;;; => first:1 second:2 rest:(3 4 5) 34 35 ;; It really is a lambda list, you can use default parameters: 36 (destructuring-bind 37 (first second &optional (third 'default)) 38 '(1 2) 39 (format t "~%~%;;; => first:~a second:~a third:~a~&" first second third)) 40 41 ;;; => first:1 second:2 third:DEFAULT 42 43 (destructuring-bind 44 (first second &optional (third 'default)) 45 '(1 2 3) 46 (format t "~%~%;;; => first:~a second:~a third:~a~&" first second third)) 47 48 ;;; => first:1 second:2 third:3 49 50 ;; And you can use keyword parameters: 51 (destructuring-bind 52 (first second &key third) 53 '(1 2 :third 3) 54 (format t "~%~%;;; => first:~a second:~a third:~a~&" first second third)) 55 56 ;;; => first:1 second:2 third:3 57 58 ;; Finally, you can use it to 'unparse' trees as well, which is a 59 ;; really great feature, since your variable declaration matches the 60 ;; 'shape' of the data strucutre you're pulling apart. This technique 61 ;; is really handy for dealing with XML after it's been converted to 62 ;; s-expressions. 63 (destructuring-bind 64 (a (b (c d e (f g) h i j)) &rest remainder) 65 '(1 (2 (3 4 5 (6 7) 8 9 10)) 11 12 13 14 15) 66 (format t 67 "~%~%;;; => a:~a b:~a c:~a d:~a e:~a f:~a g:~a h:~a i:~a j:~a remainder:~a ~&" 68 a b c d e f g h i j remainder)) 69 70 ;;; => a:1 b:2 c:3 d:4 e:5 f:6 g:7 h:8 i:9 j:10 remainder:(11 12 13 14 15)
Reference : http://asymmetrical-view.com/2008/09/18/destructuring-bind.html
S.K.