0

使用下面的代码,即使(我相信)我只在每个函数中使用局部变量,多次运行后的结果看起来像数据保留在变量中,并且最终添加了新旧结果。怎么了?

(defun funcC (target res)
  (cond
   ((null res) (list (list target 1)))
   ((equal (car (car res)) target)
    (setf (cadr (car res)) (+ (cadr (car res)) 1))
    res)
   (t (setf (cdr res) (funcC target (cdr res))) res)
  ))
(defun funcB (l res)
  (cond ((null l) nil)
    ((atom l) (funcC l res))
    ((atom (car l)) 
     (funcB (car l) res)
     (funcB (cdr l) res))
    ((listp (car l)) (funcB (car l) res))
    (t (funcB (cdr l) res)))
  res
)
(defun funcA (l)
  (cdr (funcB l '(())))
)

结果:

Break 27 [30]> lb
(10 7 7 7 4 3 7 3 10 10)
Break 27 [30]> (funcA lb)
((10 3) (7 4) (4 1) (3 2))   ; I want this result to repeat in the next run but...
Break 27 [30]> (funcA lb)
((10 6) (7 8) (4 2) (3 4))   
Break 27 [30]> (funcA lb)
((10 9) (7 12) (4 3) (3 6))  ; Each run adds up to results from previous run

环境:Ubuntu 11.10、GNU CLISP2.49

4

1 回答 1

3

这是一个经常被问到的问题。stackoverflow 上应该有这个问题的副本(原文如此!)。

funcA包含文字数据。该数据每次都被传递到funcB那里并在那里进行修改。

您应该为每次运行制作该数据的新副本。使用COPY-TREE.

另请注意,根据 Common Lisp 标准,修改文字数据可能会产生未定义的后果。例如,想象一个编译器将所有文字数据放在只读数据内存段中,或者想象一个编译器在多个地方重用相似的文字数据以节省空间。

于 2012-03-17T08:50:34.107 回答