[plt-scheme] trying not to use eval

From: Neil W. Van Dyke (neil at neilvandyke.org)
Date: Tue Oct 29 10:20:36 EST 2002

Rohan Nicholls <rohan.nicholls at informaat.nl> writes at 14:58 29-Oct-2002 +0100:
> I want to take the value from a variable and use it as a procedure name, 
>   and have it be evaluated.  I remember running across some advice in a 
> text that using eval was *BAD*, but I am stumped on how to have the code 
> evaluated otherwise.
> 
> Here is the snippet:
> 
> 	(let ((poss (eval `(,e-type ,(car lst)))))
> 		...

The way I would do it depends on how e-type gets set to the symbol.
Could e-type be set to the desired procedure instead of to a symbol?

    (define lst '(bar baz))

    (define (foo x) (format "Foo ~S!" x))

    (define e-type 'foo)
    (define e-proc foo)

    (printf "e-type holds symbol:    ~S\n" ((eval e-type) (car lst)))
    (printf "e-proc holds procedure: ~S\n" (e-proc        (car lst)))

The e-proc approach is *much* preferred to the e-type approach.

If the value of e-type is currently being set from some input that your
program is reading, you might consider instead using syntax-case to do a
rules-based translation of the input.  syntax-case *can* be used
separately from define-syntax and the transformer environment.

(syntax-case can take an hour to begin to understand, but the time
investment is worthwhile.  DrScheme makes experimenting with syntax
objects fun, by displaying them in the Interactions window with a useful
GUI snippet.)

-- 
                                                        Neil W. Van Dyke
                                             http://www.neilvandyke.org/


Posted on the users mailing list.