Common lisp 为什么不释放局部变量?

Common lisp 为什么不释放局部变量?,common-lisp,ecl,Common Lisp,Ecl,测试功能如下: (defun fab (n) (let ((res '(1 1))) (loop for i from 2 to n do (nconc res (list (+ (nth (- i 2) res) (nth (- i 1) res))))) res)) $ecl 。。。 EECL(嵌入式通用Lisp)12.7.1(git:未知) 然后我重启ECL >(fab 20) (1 1 2 3 5 8 13 21

测试功能如下:

(defun fab (n) 
    (let ((res '(1 1)))
        (loop for i from 2 to n do
            (nconc res (list (+ (nth (- i 2) res) (nth (- i 1) res)))))
        res))
$ecl

。。。 EECL(嵌入式通用Lisp)12.7.1(git:未知)

然后我重启ECL

>(fab 20)
(1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946)
在(fac 10)之后,“res”似乎没有释放

真诚的

您应该在
let
表单中使用
(列表11)
而不是
”(1 1)
。在Common Lisp中,未定义修改文字对象的效果

(定义fib(n)
让((res(list 11));(list 11)代替“(11)
(i从2到n的循环
do(NCOC res(列表(+(第n(-i 2)个res)(第n(-i 1)个res‘‘)'))
(res))
您应该在
let
表单中使用
(列表11)
而不是
”(1 1)
。在Common Lisp中,未定义修改文字对象的效果

(定义fib(n)
让((res(list 11));(list 11)代替“(11)
(i从2到n的循环
do(NCOC res(列表(+(第n(-i 2)个res)(第n(-i 1)个res‘‘)'))
(res))
编译器/解释器只分配一次诸如“(1)的常量。您的代码在此列表上使用了NCOC,正在修改,后续调用将不再看到常量列表'(1),而是已修改的列表。在CommonLisp中,当on以破坏性方式修改常量表达式时,会发生什么情况还未确定,有些实现甚至会保护它们不受更改的影响,以避免出现这种意外情况。如果您需要一个新的常量,请按照人们说的去做并使用(列表11)或避免使用NCOC。

编译器/解释器只分配一次诸如“(1 1)的常量。您的代码在此列表上使用了NCOC,正在修改,后续调用将不再看到常量列表'(1),而是已修改的列表。在CommonLisp中,当on以破坏性方式修改常量表达式时,会发生什么情况还未确定,有些实现甚至会保护它们不受更改的影响,以避免出现这种意外情况。如果你需要一个新的常量,按照人们说的去做并使用(列表1)或者避免使用NCOC

>(fab 20)
(1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946)