Common Lisp – destructuring-bind 函数的示例代码

 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.

转载于:https://www.cnblogs.com/neoshen/archive/2013/05/05/3060537.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值