[plt-scheme] eval and servlets

From: ifconfig nslookup (configurator at gmail.com)
Date: Sun Oct 3 08:55:53 EDT 2004

This has to do with environments. If I am not mistaken, when you use
eval, it uses the top-level environment. When you define 'square' in
the top-level (i.e. when running outside of the servlet), you can
reference it inside the evaluated quote. When you define it in a
module or unit, however, you would have to use the module or servlet's
environment instead.
One solution that *might* work is to transfer the definitions into the
top-level. You can do that simply by calling (eval `(define square
,square)).


Yours,
ifconfig


On Sun, 3 Oct 2004 08:14:47 -0400, David J. Neu <djneu at att.net> wrote:
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> Hi all,
> 
> I was wondering if someone could explain why in the following code,
> the commented out version of _start_ fails, with error:
> 
>    Servlet exception: "reference to undefined identifier: square"
> 
> in both a module-based and a unit-based servlet.
> 
> The "same" function works fine outside of a servlet.
> 
> Something with eval and namespaces?
> 
> Many thanks!
> --David
> 
> (module act-test
>  mzscheme
> 
>  (provide interface-version timeout start)
> 
>  (require (lib "servlet-sig.ss" "web-server")
>           (lib "servlet.ss" "web-server")
>           (lib "servlet-helpers.ss" "web-server"))
> 
>  (define interface-version 'v1)
> 
>  (define timeout +inf.0)
> 
>  (define square (lambda (x) (* x x)))
> 
>  (define ops
>    `((s . ,(lambda (x) (square x)))
>      (i . ,(lambda (x) (add1 x)))))
> 
>   ; this fails
> ;  (define start
> ;    (lambda (arequest)
> ;      (let ((lfile (open-input-file "act.ss")))
> ;        (let ((ops (eval (read lfile))))
> ;          (close-input-port lfile)
> ;          `(html
> ;            ,(format "~a" ((cdr (assq 's ops)) 2)))))))
> 
>  ; this works
>  (define start
>    (lambda (arequest)
>      `(html
>        ,(format "~a" ((cdr (assq 's ops)) 2))))))
> 
> ;; start: act.ss
> `((s . ,(lambda (x) (square x)))
>  (i . ,(lambda (x) (add1 x))))
> ;; end: act.ss
>


Posted on the users mailing list.