Start to implement 4.9

main
Felix Martin 2021-01-19 13:25:35 -05:00
parent 6b0dbdff6b
commit 522e65cec6
1 changed files with 35 additions and 0 deletions

View File

@ -214,5 +214,40 @@
(display "\nex-4.9 - iteration-constructs\n")
(define (repeat? exp) (tagged-list exp 'repeat))
(define (repeat-count exp) (cadr exp))
(define (repeat-body exp) (cddr exp))
(define (repeat->combination exp)
(if (= (repeat-count exp) 0)
'()
(list
(sequence->exp (repeat-body exp))
(cons 'repeat
(cons (- (repeat-count exp) 1)
(repeat-body exp))))))
(display "repeat: ")
(assert (repeat->combination '(repeat 10 (display "foo") (newline)))
'((begin (display "foo") (newline)) (repeat 9 (display "foo") (newline))))
(define (while? exp) (tagged-list exp 'while))
(define (while-predicate exp) (cadr exp))
(define (while-body exp) (cddr exp))
(define (while->combination exp)
(let ((body (while-body exp))
(predicate (while-predicate exp)))
(if (not (true? predicate))
'()
(list
(sequence->exp body)
(cons 'while
(cons predicate
body))))))
(display "while: ")
(display (while->combination '(while (lambda () #t) (display "one"))))
(display "\nex-4.10\n")