diff --git a/ex-4_01-xx.scm b/ex-4_01-xx.scm index ce11716..c623605 100644 --- a/ex-4_01-xx.scm +++ b/ex-4_01-xx.scm @@ -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")