Tree 在Common Lisp中使用car/cdr进行多次递归,这本书的答案表是否有误?

Tree 在Common Lisp中使用car/cdr进行多次递归,这本书的答案表是否有误?,tree,common-lisp,Tree,Common Lisp,我试图通过《CommonLisp:符号计算的温和介绍》一书来学习CommonLisp。此外,我正在使用SBCL、Emacs和Slime 在第八章的中间,作者介绍了树上的递归。他通过树上的一个函数展示了这个概念,该函数在树的所有非列表元素中插入符号'q: 我在我的环境中也做了同样的事情: (defun atoms-to-q (xs) (cond ((null xs) nil) ((atom xs) 'q) (t (cons (atoms-to-q (car x

我试图通过《CommonLisp:符号计算的温和介绍》一书来学习CommonLisp。此外,我正在使用SBCL、Emacs和Slime

在第八章的中间,作者介绍了树上的递归。他通过树上的一个函数展示了这个概念,该函数在树的所有非列表元素中插入符号
'q

我在我的环境中也做了同样的事情:

(defun atoms-to-q (xs)
  (cond ((null xs) nil)
        ((atom xs) 'q)
        (t (cons (atoms-to-q (car xs))
                 (atoms-to-q (cdr xs))))))
我得到了同样的结果:

CL-USER> (atoms-to-q '(a . b))
(Q . Q)
CL-USER> (atoms-to-q '(hark (harold the angel) sings))
(Q (Q Q Q) Q)
CL-USER> (atoms-to-q-no-null '(a (b) c))

(Q (Q . Q) Q . Q)
然后这本书问道:

同一本书提供了一个伟大的aswer表。作为对这个具体问题的回答:

8.38。如果省略第一个COND子句,cons单元链末端的NILs也将转换为Qs。所以(原子到Q’(A(B)C))将返回(A(B.Q)C.Q)

现在我感到困惑了。在我的环境中,我删除了第一条:

(defun atoms-to-q-no-null (xs)
  (cond ((atom xs) 'q)
        (t (cons (atoms-to-q (car xs))
                 (atoms-to-q (cdr xs))))))
在使用书的答案表上建议的输入运行函数后,我得到了一个不同的结果:

CL-USER> (atoms-to-q-no-null '(a (b) c))
(Q (Q) Q)

不幸的是,在应付和唠叨时,我忘记了将atoms-to-q-no-null的递归调用更改为
。我在为stackoverflow准备这个问题时意识到了这个错误

因为我已经写了问题的一部分,所以我认为最好是回答它,并让这些努力对其他人有用

修复函数的递归调用后:

(defun atoms-to-q-no-null (xs)
  (cond ((atom xs) 'q)
        (t (cons (atoms-to-q-no-null (car xs))
                 (atoms-to-q-no-null (cdr xs))))))
我得到了同样的结果:

CL-USER> (atoms-to-q '(a . b))
(Q . Q)
CL-USER> (atoms-to-q '(hark (harold the angel) sings))
(Q (Q Q Q) Q)
CL-USER> (atoms-to-q-no-null '(a (b) c))

(Q (Q . Q) Q . Q)
顺便说一句,这是一本非常有趣而且写得很好的书