[plt-scheme] a couple of questions

From: Gregory Woodhouse (gregory.woodhouse at sbcglobal.net)
Date: Fri Jan 12 08:07:16 EST 2007

I'm trying to write an evaluator in the style of SICP that I hope  
will be at least somewhat idiomatic. What I have looks like this

;some global definitions (never modified)
(define evaluator-name "Strict Evaluator")
(define evaluator-version "0.0.1")
(define main-prompt ">>")
(define exit-message "Goodbye!")

(define (eval-strict exp env) (raise-user-error 'oops))

(define (run)
   (let
       ((env '())
        (input-val (void)))
     (printf "~a (version ~a)~n" evaluator-name evaluator-version)
     (let
         loop ()
       (with-handlers
           ((exn:fail:user? (lambda (exn) (printf "Error!~n") (loop))))
           (printf "~a " main-prompt)
           (set! input-val (read))
           (if (not (eof-object? input-val))
               (begin
                 (printf "~a~n~n" (eval-strict input-val env))
                 (loop))
               (printf "~a~n" exit-message))))))

That code makes me uncomfortable for a couple of reasons

1. I don't want to break out of the loop when evaluating an  
expression like (/ 1 0) but instead report the error and then give  
the user an opportunity to enter another expression, leaving the  
environment intact. But, of course, I do want to break out of the  
loop in case of a programming error on my part, so my handler for  
exn:fail:user includes (loop) (where loop is the label in named let).  
I've never encountered code like this, and I wonder if it's even  
safe, much less idiomatic scheme.

2. The second question involves the environment, which I anticipate  
storing as a list or perhaps a hash table.  To me, it makes more  
sense for the evaluator to return an updated environment as a value  
than to have it modify a global value. But it also seems that this  
style could involve a lot of unnecessary copying. A third alternative  
I've considered is returning a list of updates that would then be  
sent on to a global environment object before proceeding  
(hmm...shadows of multithreaded scheme?) On the other hand, this  
approach sounds complex. I guess my question is whether I'm missing  
something obvious here. This all seems like it ought to be much more  
straightforward.


Gregory Woodhouse
gregory.woodhouse at sbcglobal.net

"We may with advantage at times forget what we know."
--Publilius Cyrus, c. 100 B.C.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070112/9af7b17c/attachment.html>

Posted on the users mailing list.