关于 Common Lisp 中 car/cdr 的多次递归,这本书的答案 - sheet 是错误的吗?

Is the book answer-sheet wrong about this multiple recursion with car/cdr in Common Lisp?

我正在尝试通过 Common Lisp:对符号计算的简单介绍 来学习 Common Lisp。此外,我正在使用 SBCL、Emacs 和 Slime。

在第8章中间,作者介绍了树的递归。他用树上的一个函数展示了这个概念,该函数在树的所有非列表元素中插入符号 '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)

然后书上问:

同一本书提供了很好的答案sheet。显示为这个特定问题的答案:

8.38. If the first COND clause is omitted, the NILs at the end of cons cell chains will also be converted to Qs. So (ATOMS-TO-Q ’(A (B) C)) will return (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))))))

并且在 运行 带有书中答案中建议的输入的函数之后 sheet 我得到 不同的结果 :

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

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

既然我已经写了问题的一部分,我想最好还是回答一下,让这些努力对其他人有用。

修复函数的递归调用后:

(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-no-null '(a (b) c))

(Q (Q . Q) Q . Q)

顺便说一句,这是一本非常有趣且写得很好的书。