[SPAM] [plt-scheme] Novice question: different behavior in def and eval windows

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Thu Jan 7 09:57:46 EST 2010

This is not a bug. You are here relying on behavior of the current
namespace and you've not initialized it at all.

That is, the argument to 'eval' is evaluated in an environment that is
outside of any module whereas code in the definitions window (or in
any file anywhere) [*] is inside some module which is where the
current bindings come from.

There are a number of ways to rewrite the code below to something that
does not depend on this extra-module environment and, as a rule, our
tools all work better when you do that (tools meaning the compiler,
check syntax, the module browser, etc etc). It is difficult to get
such tools going in a world with eval. Sometimes eval is necessary
(say, when you implement a program development environment and want to
run a program that, say, appears in the definitions window), but for
most programs you can and should avoid it.

The simplest way to rewrite the below would be to make a table mapping
those symbols to the corresponding functions. I'm going to guess that
you don't like that, tho, because you want to avoid writing both
"year" and "date-year". The better way to do this would be to use a
macro that, at compile time, constructs the identifiers that you are
after and then builds code for the table, for use at runtime. (This
approach is well-supported by our tools.)


[*] this is true only for the HtDP teaching languages, the module
language (and many others); it is not true for some of the legacy
languages. But I don't recommend using those languages; they are
legacy for a reason. :)

On Thu, Jan 7, 2010 at 7:48 AM, Mike G. <mikeg at psg.com> wrote:
> I don't know if this is a bug in Dr. Scheme or a programming
> subtlety that escapes me.  Given this definition in the
> definitions window,
> (define selectors
>  (map (λ (a-symbol) (string->symbol (string-append "date-" (symbol->string a-ssymbol))))
>       '(year
>         month
>         day
>         week-day
>         hour
>         minute
>         second)))
> The expression (map eval selectors) in the evaluations window
> gives me a list of procedures, but in the definitions window
> gives the error message:
> compile: unbound identifier (and no #%top syntax transformer is bound) in: date-year
> I am using DrScheme, version 4.2.3, english.
> Thanks,
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.