[plt-scheme] Problems with eval

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Jul 3 15:19:51 EDT 2008

On Jul  3, Robby Findler wrote:
> This is not a bug.
> The top-level namespace (ie the part of the namespace that deals
> with code outside modules) for the module language does not have any
> bindings. You have to initialize it with some if you want to use
> them.
> It would be better to avoid eval (as has been mentioned a few times
> on this list) but on the assumption that eval is truely necessary,
> try this:
>   #lang scheme
>   (namespace-require 'scheme/base)
>   (eval '((lambda (x) x) 1))
> Without the second line, the third one won't work, but with it, it
> does.

You can also use a private namespace

  #lang scheme
  (define ns (make-base-namespace))
  (eval '((lambda (x) x) 1) ns)

or even better, you can use the namespace of the current module:

  #lang scheme
  (define-namespace-anchor anchor)
  (eval '((lambda (x) x) 1) (namespace-anchor->namespace anchor))

and you can even do this now:

  #lang scheme
  (define-namespace-anchor anchor)
  (define c 1)
  (eval '((lambda (x) x) c) (namespace-anchor->namespace anchor))

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.