Ex3.4 リストを表示

printの様な関数を作成する。ドット対表記が必要ないところは通常のリスト表記で表示する。

結構力業。

;;; Exercise 3.4 [m] Write a function that, like the regular print
;;; function, will print an expression in dotted pair notation when
;;; necesary but will use normal list notation when possible.
(defun my-print (expression &optional in-list)
  "print an expression in dotted pair notation."
  (if (atom expression) (write expression)
      (progn
	(unless in-list
	  (princ "("))
	(my-print (car expression))
	(cond 
	  ((null (cdr expression)))
	  ((listp (cdr expression))
	       (princ " ") (my-print (cdr expression) t))
	  (t (princ " . ") (my-print (cdr expression) t)))
	(unless in-list
	  (princ ")"))
	(cons (car expression) (cdr expression)))))


(my-print '(a b (c . d) e))
;; (A B (C . D) E) ; printed
;; => (A B (C . D) E)
(my-print '(a b (c . d) e ()))
;; (A B (C . D) E NIL) ; printed
;; => (A B (C . D) E NIL)