[plt-scheme] How to execute code in an environment other than it is defined?

From: Grant Rettke (grettke at acm.org)
Date: Sun Jul 15 01:23:55 EDT 2007

Hi folks,

I am trying to solve a problem and in doing so have found a more
general question. I'm not quite sure I'm usingthe correct terminology
on this one because originally I was thinking about executing
environments, but perhaps the questions is more about using eval... so
here is an example of what I would like to do (using the pretty big
language in DrS):

(require (lib "42.ss" "srfi"))
(define (run1)
  (list-ec (:range x 1 11) (if (even? x)) (identity x)))

>(run1) -> (2 4 6 8 10)

Suppose that in run1 it would make sense for the client of the
function to pass in a filter method. Here is one approach:

(define (run2 filter)
  (list-ec (:range x 1 11) (if (filter x)) (identity x)))

>(run2 (λ (x) (even? x))) -> (2 4 6 8 10)

Suppose that the user doesn't want to have to write a function,
though, and could do something like this instead:

>(run2 '{even? x}}

so that when run2 executes, that expression could be executed within
the environment of run2. Is this possible? How is it possible? Is it a
reasonable thing to do?

My early morning guess was wrong:

(define (run3 filter)
  (list-ec (:range x 1 11) (if (eval filter)) (identity x)))

>(run3 '(even? x))
>--reference to undefined identifier: x

Posted on the users mailing list.