[plt-scheme] trying not to use eval

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Tue Oct 29 09:38:02 EST 2002

Rohan Nicholls wrote:

> 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)))))
> ...
> Is there a way to do this without using eval and/or am I incorrect in
> thinking of eval as something to be avoided?

You could look up the functions in a table like this:

> (define names `( (add  . ,+)
                   (sub  . ,-)
                   (mult . ,*) ))

> (assoc 'sub names )
(sub . #<primitive:->)

> (define (name->op name)
    (cdr (assoc name names)))

> ((name->op 'sub) 4 3)

PS: Happy birthday, Matthias!
Jens Axel Søgaard

Posted on the users mailing list.