【发布时间】:2020-02-11 15:16:28
【问题描述】:
我是 Emacs lisp 的初学者,所以这确实是一个菜鸟问题。假设我必须编写一个函数,该函数使用循环将数字向量的每个元素加 1。
这是我编写的代码(cmets 表示我在每个步骤中尝试做什么):
(defun add-one (x)
"Use a loop to add 1 to each element of list X"
(let* ((res x) ; make a copy of X
(counter 0)) ; set counter to 0
(while (< counter (length x))
;; Replace each element of RES by adding 1:
(setcar (nthcdr counter res) (1+ (car (nthcdr counter x))))
(setq counter (1+ counter)))
;; Display result:
(message "%s" res)))
但我的代码似乎对x 具有破坏性,因为对该函数的多次调用不会产生相同的结果:
;; Define a list:
(setq mylist '(1 2 3 4))
;; Several calls to the function:
(add-one mylist) ; -> (2 3 4 5)
(add-one mylist) ; -> (3 4 5 6)
这是我的问题:我不明白为什么我的代码具有破坏性(我预计每次执行的结果都是(2 3 4 5))。我知道setcar 具有破坏性,但它适用于x 的副本,而不是x 本身。那么为什么结果会发生变化呢?
谢谢!
【问题讨论】: