I am trying to learn Common Lisp with the book Common Lisp: A gentle introduction to Symbolic Computation. In addition, I am using SBCL, Emacs, and Slime.
In the middle of chapter 8, the author presents recursion on trees. He showcases this concept with a function on trees that inserts the symbol 'q in all non-list elements of the tree:
I did the same in my environment:
(defun atoms-to-q (xs)
(cond ((null xs) nil)
((atom xs) 'q)
(t (cons (atoms-to-q (car xs))
(atoms-to-q (cdr xs))))))
And I got the same results:
CL-USER> (atoms-to-q '(a . b))
(Q . Q)
CL-USER> (atoms-to-q '(hark (harold the angel) sings))
(Q (Q Q Q) Q)
Then the book asks:
The same book provides a great aswer sheet. Which shows as an answer for this specific question:
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)
Now we reach the point of confusion for me. In my environment I removed the first clause with:
(defun atoms-to-q-no-null (xs)
(cond ((atom xs) 'q)
(t (cons (atoms-to-q (car xs))
(atoms-to-q (cdr xs))))))
And after running the function with inputs suggested on the book's answer sheet I get a different result:
CL-USER> (atoms-to-q-no-null '(a (b) c))
(Q (Q) Q)

