lisp遍历表中所有顶点_common-lisp – Lisp-循环遍历列表并替换值

在不使用循环的情况下执行相同操作的另一种方法(尽管它在概念上类似)

(defun add-two-lists (a b c &optional (d c))

(if a

(add-two-lists

(cdr a) (cdr b)

(cdr (rplaca c (+ (car a) (car b)))) d) d))

(add-two-lists '(1 2 3 4 5) '(1 2 3 4 5) '(nil nil nil nil nil))

编辑

编辑2

但请注意优化的版本行为.可能,再次,YMMV,但这是我用SBCL获得的64位Debian.

(defun add-two-lists (a b c &optional (d c))

(if a

(add-two-lists

(cdr a) (cdr b)

(cdr (rplaca c (+ (car a) (car b)))) d) d))

(time

(dotimes (i 1e6)

(add-two-lists '(1 2 3 4 5)

'(1 2 3 4 5)

'(nil nil nil nil nil))))

;; Evaluation took:

;; 0.077 seconds of real time

;; 0.076004 seconds of total run time (0.076004 user, 0.000000 system)

;; 98.70% CPU

;; 214,723,476 processor cycles

;; 0 bytes consed

(defun add-two-lists-1 (list1 list2 list3)

(loop for a in list1

for b in list2

for c on list3 do

(rplaca c (+ a b))))

(time

(dotimes (i 1e6)

(add-two-lists-1 '(1 2 3 4 5)

'(1 2 3 4 5)

'(nil nil nil nil nil))))

;; Evaluation took:

;; 0.060 seconds of real time

;; 0.(defun add-two-lists (a b c &optional (d c))

(if a

(add-two-lists

(cdr a) (cdr b)

(cdr (rplaca c (+ (car a) (car b)))) d) d))

(add-two-lists '(1 2 3 4 5) '(1 2 3 4 5) '(nil nil nil nil nil))

4 seconds of total run time (0.

(defun add-two-lists (a b c &optional (d c))

(if a

(add-two-lists

(cdr a) (cdr b)

(cdr (rplaca c (+ (car a) (car b)))) d) d))

(add-two-lists '(1 2 3 4 5) '(1 2 3 4 5) '(nil nil nil nil nil))(defun add-two-lists (a b c &optional (d c))

(if a

(add-two-lists

(cdr a) (cdr b)

(cdr (rplaca c (+ (car a) (car b)))) d) d))

(add-two-lists '(1 2 3 4 5) '(1 2 3 4 5) '(nil nil nil nil nil))(defun add-two-lists (a b c &optional (d c))

(if a

(add-two-lists

(cdr a) (cdr b)

(cdr (rplaca c (+ (car a) (car b)))) d) d))

(add-two-lists '(1 2 3 4 5) '(1 2 3 4 5) '(nil nil nil nil nil))4 user, 0.000000 system)

;; 100.00% CPU

;; 169,395,444 processor cycles

;; 0 bytes consed

(defun add-two-lists (a b c &optional (d c))

(declare (optimize (speed 3) (safety 0)))

(declare (type list a b c d))

(if a

(add-two-lists

(cdr a) (cdr b)

(cdr (rplaca

c

(the fixnum

(+ (the fixnum (car a))

(the fixnum (car b)))))) d) d))

(time

(dotimes (i 1e6)

(add-two-lists '(1 2 3 4 5)

'(1 2 3 4 5)

'(nil nil nil nil nil))))

;; Evaluation took:

;; 0.041 seconds of real time

;; 0.040002 seconds of total run time (0.040002 user, 0.000000 system)

;; 97.56% CPU

;; 114,176,175 processor cycles

;; 0 bytes consed

(defun add-two-lists-1 (list1 list2 list3)

(declare (optimize (speed 3) (safety 0)))

(loop for a fixnum in list1

for b fixnum in list2

for c cons on list3 do

(rplaca c (the fixnum (+ a b)))))

(time

(dotimes (i 1e6)

(add-two-lists-1 '(1 2 3 4 5)

'(1 2 3 4 5)

'(nil nil nil nil nil))))

;; Evaluation took:

;; 0.040 seconds of real time

;; 0.040003 seconds of total run time (0.040003 user, 0.000000 system)

;; 100.00% CPU

;; 112,032,123 processor cycles

;; 0 bytes consed

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值