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

From: Kyle Smith (airfoil at bellsouth.net)
Date: Sun Jul 15 16:23:45 EDT 2007

Hi Grant and Jos,

I believe you can construct the syntax such that no extraneous
variable need to be added.  Check out my take on your syntax
below.  I've also corrected for the annoying attribute of range
that acts like a C `for' statement, in that if you specify a range
of from 1 to 100, you actually get 1 to 99.  Finally, I added
fenders and syntax/loc to provide for more specific error
reporting.  You could also match on the lambda glyph, but
they don't show up on the newsgroup well, so I left it out.

(module pick-from mzscheme
  (require (lib "42.ss" "srfi"))
  (provide pick-from)
  (define (iter f from to)
    (list-ec  (:range x from (add1 to))
              (if (f x))
              x))
  (define-syntax pick-from
    (lambda (stx)
      (syntax-case stx ()
        [(_ (lambda (var) expr) from to)
         (identifier? #'var)
         (syntax/loc stx
           (iter (lambda (y) ((lambda (var) expr) y)) from to))]
        [(_ (lambda (var) expr) to)
         (identifier? #'var)
         (syntax/loc stx
           (iter (lambda (y) ((lambda (var) expr) y)) 1 to))]
        [(_ proc (from to))
         (identifier? #'proc)
         (syntax/loc stx
           (iter proc from to))]
        [(_ proc to)
         (identifier? #'proc)
         (syntax/loc stx
           (iter proc 1 to))]))))

(require pick-from)
(pick-from  (lambda (a) (zero? (modulo a 3))) 50 100)
(pick-from even? 100)
(pick-from odd? 100)
(define triple? (lambda (a) (zero? (modulo a 3))))
(pick-from triple? 100)

Enjoy!

--kyle
airfoil at bellsouth dot net
schemekeys.blogspot.com
www.schemekeys.net


Posted on the users mailing list.