[plt-scheme] environment questions

From: Anton van Straaten (anton at appsolutions.com)
Date: Fri Jul 18 19:27:23 EDT 2003

> Is "namespace" in PLT the functional equivalent of "environment" in SICP?

No.  A namespace only corresponds to a top-level environment, what SICP
calls the global environment.

SICP environments are "a sequence of frames".  In Scheme, all frames
"enclosed" by the global environment are lexical ones.  In typical Scheme
implementations, the names of variables in a lexical frame are not available
at runtime.  They only exist in the syntax of the source program.

In general, if you need to do anything that involves knowing the names of
lexical variables, you're going to need to do something "before" runtime,
e.g. with macros, or by analyzing code yourself.

With PLT Scheme, if you need access to lexical variable info, take a look at
the MzScheme Language Manual, Chapter 12, Syntax and Macros.  This covers
the API for manipulating syntax, including lexical variable names.

For example, here I've used quote-syntax to quote a program fragment, and
then used PLT's syntax API to extract the lexical variable name from that
syntax object:

  (define stx (quote-syntax (let ((foo 99)) foo)))
  (define var-list-stx (cadr (syntax-e stx))) ; ((foo 99))
  (define var-clause-stx (car (syntax-e var-list-stx))) ; (foo 99)
  (define var (car (syntax-e var-clause-stx))) ; foo

  (identifier? var)           ; => #t
  (syntax-object->datum var)  ; => foo


Posted on the users mailing list.