Scheme - eval如何识别变量?

时间:2015-10-11 07:12:35

标签: variables scheme eval

例如,如果我定义一个函数:

(define (^ x y) (expt x y))

然后eval可以识别(^ 3 3)并对其进行评估,但是我需要它来评估从输入中读入的变量(这些变量不是常量,这意味着如果读取相同的变量,可以稍后更改它们具有不同的价值)。

eval有没有办法识别变量?

1 个答案:

答案 0 :(得分:0)

如何使用eval的确切约定因实现而异 - 所以最好的建议是在Scheme实现的文档中查找eval

据说R5RS中关于eval的官方文档说:

procedure:  (eval expression environment-specifier) 

Evaluates expression in the specified environment and returns its value. 
Expression must be a valid Scheme expression represented as data, 
and environment-specifier must be a value returned by one of the three
procedures described below. Implementations may extend eval to allow non-
expression programs (definitions) as the first argument and to allow other
values as environments, with the restriction that eval is not allowed to 
create new bindings in the environments associated with null-environment or 
scheme-report-environment.


(eval '(* 7 3) (scheme-report-environment 5))
                                                           ===>  21

(let ((f (eval '(lambda (f x) (f x x))
               (null-environment 5))))
  (f + 10))
                                                           ===>  20