Fw: [plt-scheme] How to execute code in an environment other than it isdefined?
Forgot to cc this to plt.
Jos Koot
----- Original Message -----
From: "Jos Koot" <jos.koot at telefonica.net>
To: "Grant Rettke" <grettke at acm.org>
Sent: Sunday, July 15, 2007 10:49 AM
Subject: Re: [plt-scheme] How to execute code in an environment other than it
isdefined?
> Hi,
> For the example you provide, you can simply write
>
> (require (lib "42.ss" "srfi"))
>
> (define (run filter)
> (list-ec
> (:range x 1 11)
> (if (filter x))
> x))
>
> (run even?)
>
> If I understand your question well, you want
> (run2 (lambda (x) more-complicated-expression-in-x))
> to be abbreviated as
> (run2 more-complicated-expression-in-x)
>
> This is possible by means of a macro, but I dont find it elegant, because you
> will have to choose a fixed name for variable x. The problem becomes much
> easier if you are willing to force the user to mention the name of the
> variable, for example: (run x more-complicated-expression-in-x)
>
> (module run mzscheme
> (require (lib "42.ss" "srfi"))
> (define (runner filter)
> (list-ec
> (:range x 1 11)
> (if (filter x))
> x))
> (define-syntax run
> (syntax-rules ()
> ((run var expr) ; <==
> (runner (lambda (var) expr)))
> ((run proc) ; allow a simple proc too
> (runner proc))))
> (provide run))
>
>
> (require run)
>
> (run x (zero? (modulo x 3))) ; <==
> ; The following still works too:
> (run even?)
> (run odd?)
> (define triple? (lambda (x) (zero? (modulo x 3))))
> (run triple?)
>
> In general macros are to be preferred in order to evaluate pieces of code in
> the appropriate context. It is possible to prepare a macro that does not
> require specification of the name of the variable, but in this case the user
> must be forced to use a fixed name for the variable, say 'x'. The macro will
> have to parse the expression for free occurrences of x. And of course problems
> are likely to occur, for the user may already have a variable of that name as
> in:
>
> (define x 5)
> (run (> x x)) ??? which x is which ???
>
> For the above macro we have:
> (run y (> x y)) or
> (run y (> y x))
>
> Jos Koot
>
>
> ----- Original Message -----
> From: "Grant Rettke" <grettke at acm.org>
> To: "pl >> mzscheme Mailing List" <plt-scheme at list.cs.brown.edu>
> Sent: Sunday, July 15, 2007 7:23 AM
> Subject: [plt-scheme] How to execute code in an environment other than it
> isdefined?
>
>
>> 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
>>
>
>
> --------------------------------------------------------------------------------
>
>
>> _________________________________________________
>> For list-related administrative tasks:
>> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>