-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpprint.lisp
More file actions
41 lines (38 loc) · 1.53 KB
/
Copy pathpprint.lisp
File metadata and controls
41 lines (38 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
; Pretty-prints nested s-expressions, indenting list elements per depth and handling dotted tails.
; User-facing entry point: defaults indent to 0.
(define (pprint s-expression)
(pprint-inner s-expression 0)
nil)
(define (pprint-inner s-expression indent)
((lambda (new-indent)
(if (atom s-expression)
(princ s-expression)
(begin
(princ "(")
(pprint-inner (car s-expression) new-indent)
; Iterate through the tail so lists and dotted pairs are handled uniformly.
((lambda (iter)
(iter iter (cdr s-expression) nil))
(lambda (self rest multiline)
(if (not rest)
(begin
; When we already emitted a tail element we need a newline before ')'.
(if multiline
(begin
(format t "~%")
(format t "~v@t" indent))
nil)
(princ ")"))
(if (atom rest)
; Dotted tail – print " . tail" inline and close immediately.
(begin
(princ " . ")
(pprint-inner rest new-indent)
(princ ")"))
(begin
(format t "~%")
(format t "~v@t" new-indent)
(pprint-inner (car rest) new-indent)
(self self (cdr rest) t)))))))))
(+ indent 2))
nil)