5.8. Eval


5.8. Eval

Scheme's eval procedure allows programmers to write programs that construct and evaluate other programs. This ability to do meta programming should not be overused but is extremely handy when needed.

eval and the environment specifiers appear in the Revised5 Report but not in the the ANSI/IEEE standard.

(eval obj env-spec)

procedure

returns: value of the Scheme form represented by obj

env-spec must be an environment specifier returned by interaction-environment, scheme-report-environment, or null-environment. eval treats obj as the representation of an expression. It evaluates the expression in the specified environment and returns its value.

 (define cons 'not-cons) (eval '(let ((x 3)) (cons x 4))       (scheme-report-environment 5))  (x . 4) (define lambda 'not-lambda) (eval '(lambda (x) x) (null-environment))  #<procedure> (eval '(cons 3 4) (null-environment))  error 

An implementation may extend eval to support other environments. An implementation may also permit obj to be the representation of a definition, but eval must not allow the creation of new bindings in a null or scheme-report environment. The effect of assigning (through the use of eval) a variable bound in a scheme-report environment is unspecified; thus, the environment may be immutable.

(scheme-report-environment version)

procedure

(null-environment version)

procedure

returns: see below

version must be an exact integer. This integer specifies a revision of the Revised Report on Scheme, i.e., the Revisedv Report on Scheme for version v.

scheme-report-environment returns a specifier for an environment that is empty except for all bindings defined in the specified report that are either required or both optional and supported by the implementation. null-environment returns a specifier for an environment that is empty except for the bindings for all syntactic keywords defined in the specified report that are either required or both optional and supported by the implementation.

scheme-report-environment and null-environment must accept the value of version that corresponds to the most recent Revised Report that the implementation claims to support, starting with the Revised5 Report. They may also accept other values of version. An error is signaled if the implementation does not support the given version.

(interaction-environment)

procedure

returns: an environment specifier

interaction-environment returns an environment specifier that represents an environment containing implementation-dependent bindings. This environment is typically the one in which the implementation evaluates expressions dynamically typed by the user.




The Scheme Programming Language
The Scheme Programming Language
ISBN: 026251298X
EAN: 2147483647
Year: 2003
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net