Metacircular Interpreter while loop in scheme

437 views Asked by At

Why doesn't this while loop work for the metacircular interpreter. How do i run this in my interactions window ?

((while? exp) (eval (while->combination  exp) env)) 

(define (while-condition expr) (cadr expr))
 (define (while-body expr) (caddr expr))

 (define (while->combination exp)
         (sequence->exp
                 (list (list 'define  
                                 (list 'while-iter) 
                                         (make-if (while-condition exp)  
                                                          (sequence->exp (list (while-body exp)  
                                                                                        (list 'while-iter))) 
                                                          'true))
                           (list 'while-iter))))
1

There are 1 answers

0
Sylwester On

To answer this I have used the metacircular evaluator described in SICP 4.1.1 since your code lacked some procedures in order to run.

So if you have a test form like (while (call-something) (do-something-1) (do-something-2))) you can send it quoted to while->combination like this:

;; NB: notice I quote the code
(while->combination '(while (call-something) 
                       (do-something-1)
                       (do-something-2))) 

If you are using DrRacket you can just put the same at the end of the definitions window and hit Debug >| and step through your code easily.

The output I'm getting from running this is:

(begin 
  (define (while-iter) 
    (if (call-something) 
        (begin (do something-1) 
               (while-iter))
        true)) 
  (while-iter))