Ex3.3 ドット対でリストを表示

問題ではprincを使えと書いてあるけど、writeで実装した。
せっかくなので作成する関数も元のリストをconsし直して返す様にしてみた。

;;; Exercise 3.3 [m] Write a function that will print an expression in
;;; dotted pair noteation. Use the bulit-in function princ to print
;;; each component of the expression.
(defun print-dotted-pair (expression)
  "print an expression in dotted pair notation."
  (if (atom expression) (write expression)
      (progn
	(princ "(")
	(print-dotted-pair (car expression))
	(princ " . ")
	(print-dotted-pair (cdr expression))
	(princ ")")
	(cons (car expression) (cdr expression)))))
	 
(print-dotted-pair '(a b c))
;; (A . (B . (C . NIL))) ;; printed
;; => (A B C)
(print-dotted-pair '(a))
;; (A . NIL) ;; printed
;; => (A)
(print-dotted-pair '(a . b))
;; (A . B) ;; printed
;; => (A . B)
(print-dotted-pair '(a b "c"))
;; (A . (B . ("c" . NIL))) ;; printed
;; => (A B "c")