Scheme - 我试图扩展此声明有什么问题?

时间:2012-03-29 05:26:04

标签: scheme environment racket

这是一个家庭作业问题。

问题 enter image description here

我的尝试(整个文件):http://pastebin.com/TS6mByEj

如果你搜索let var = exp1 in body,那就是我需要根据问题扩展的功能。

当我测试上面的示例代码时,出现错误apply-env: No binding for y

  

(eval"设x = 30                  in let x = - (x,1)                         y = - (x,2)                     in - (x,y)")

; The following is execution log

The-next-two-lines-shows-var-and-exp1
(x)
(#(struct:const-exp 30))

diff-exp
#(struct:var-exp x)
#(struct:const-exp 1)

diff-exp
#(struct:var-exp x)
#(struct:const-exp 2)

The-next-two-lines-shows-var-and-exp1
(x y)
(#(struct:diff-exp #(struct:var-exp x) #(struct:const-exp 1)) #(struct:diff-exp #(struct:var-exp x) #(struct:const-exp 2)))

diff-exp
#(struct:var-exp x)
#(struct:var-exp y)

我知道这是一种非常长的语言,但是如果有人能够引导我走向正确的方向那将是非常好的。

谢谢!


更新

在我评估并点击错误之前,新环境env1就像这样

(#(struct:extend-env x #(struct:num-val 29) #(struct:extend-env x #(struct:num-val 30) #(struct:extend-env i #(struct:num-val 1) #(struct:extend-env v #(struct:num-val 5) #(struct:extend-env x #(struct:num-val 10) #(struct:empty-env))))))

#(struct:extend-env y #(struct:num-val 28) #(struct:extend-env x #(struct:num-val 30) #(struct:extend-env i #(struct:num-val 1) #(struct:extend-env v #(struct:num-val 5) #(struct:extend-env x #(struct:num-val 10) #(struct:empty-env)))))))

其中一个是#(struct:extend-env y #(struct:num-val 28)。因此y存在于将要评估的环境中,除了它不是car env1的一部分。它位于cdr env1

然而,我的代码依赖于car env1 ....

1 个答案:

答案 0 :(得分:4)

你已经掌握了问题,但你的语言暗示了一些概念上的问题。特别是,env1不是环境,而是环境列表。你为什么使用map?你对结果的car有什么看法?如果您在"let in 5"上运行解释器(即没有变量绑定)会发生什么?

您对mapcar的使用向我建议您尝试在自动驾驶上进行编码(“我有一个列表... map会对列表执行操作!”) 。无论是那个,还是你认为extend-env 改变(改变)环境,map是一种多次改变的方式。但那是错的。

我的建议:考虑一下你想要的环境。创建一个单独的辅助函数来计算新环境。使它成为一个简单的递归函数:没有像map那样的高阶助手(还)。为它编写测试用例。一旦你使它工作(即测试),看看它是否适合你可以使用更高阶函数来简化的模式。