If you ever implement your own LISP and need to do this remember than what you are asking actually is simpler than displaying lists as lists rather than dotted pair notation. Here's one way to do it:
(define (my-display x)
(let recur ((x x) (list-mode #f))
(cond ((pair? x)
(display (if list-mode " " "("))
(recur (car x) #f)
(recur (cdr x) #t))
((not list-mode) (display x))
((null? x) (display ")"))
(else
(display " . ")
(display x)
(display ")")))))
You see that we need to keep a state telling us that we are processing a list since how it looks depend on the cdr
. What you want is to treat all pairs the same even when the cdr
is ()
or pair?
so it's much simpler:
(define (my-display x)
(let recur ((x x))
(cond ((pair? x)
(display "(")
(recur (car x))
(display " . ")
(recur (cdr x))
(display ")"))
(else (display x)))))
Of course a real implementation of this might have much more logic to display the different atomic values so thanks to that these actually just address the pairs. When making my own Lisp Zozotez print was the second largest function after read.