-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex-4.34.scm
58 lines (50 loc) · 1.69 KB
/
ex-4.34.scm
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(load "./sec-4.2.3.scm")
(define lazy-printable
'(
(define (cons x y)
(lambda (m) (m lazy x y))) ; lazy が 'lazy でないことに注意!
(define (car z)
(z (lambda (i p q) p)))
(define (cdr z)
(z (lambda (i p q) q)))
))
(force-exps lazy-printable)
(define (lazy-pair? input)
(cond ((not (pair? input)) #f)
((not (eq? (car input) 'procedure)) #f)
(else
(eq? 'lazy (cadar (procedure-body input))))))
(define (procedure->lambda proc)
(make-lambda (procedure-parameters proc)
(procedure-body proc)))
(define (print-object object)
(define (display-delayed-cell cell)
(if (lazy-pair? cell)
(display "<lazy-obj>")
(display cell)))
(cond ((lazy-pair? object)
(display "(")
(display-delayed-cell (actual-value `(car ,(procedure->lambda object))
(procedure-environment object)))
(display ".")
(display-delayed-cell (actual-value `(cdr ,(procedure->lambda object))
(procedure-environment object)))
(display ")"))
((compound-procedure? object)
(display (list 'compound-procedure
(procedure-parameters object)
(procedure-body object)
'<procedure-env>)))
(else (display object))))
(define test-exps
'(cons 1 (cons 2 '())) ; -> (1 '())
; '(lambda (lazy) (list lazy))
; '(print-object (cons 1 '()))
; '(print-object (cons 1 (cons 2 '())))
)
(define (user-print object)
(print-object object)
(newline))
(define (main args)
(print-object (actual-value test-exps the-global-environment))
)