Start to implement 4.9
This commit is contained in:
@@ -214,5 +214,40 @@
|
|||||||
|
|
||||||
(display "\nex-4.9 - iteration-constructs\n")
|
(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")
|
(display "\nex-4.10\n")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user