2012-03-29 63 views
1

這是一個家庭作業問題。計劃 - 我試圖擴展此聲明有什麼問題?

問題 enter image description here

我嘗試(整個文件):http://pastebin.com/TS6mByEj

如果你搜索let var = exp1 in body,這就是我需要根據問題來擴展功能。

當我測試上述樣本代碼,我得到一個錯誤apply-env: No binding for y

(EVAL「令x = 30 中設X = - (X,1) Y = - (X,2) 在 - (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) 

我知道這是很長的語言,但如果任何人都可以親切地帶領我到正確的方向將是非常非常好的。

謝謝!


UPDATE

之前,我評估和命中誤差,新的環境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

對不起,我刪除了我的答案,因爲我誤解了你的問題,而我的答案不會有任何幫助。 – 2012-03-29 05:39:33

+0

@KshitijMehta沒關係。你仍然花時間回覆:)讓我們希望有人能夠在這裏給我一個好頭!謝謝。 – CppLearner 2012-03-29 05:40:52

回答

4

你必須對這個問題你的手指,但是你的語言提出了一些概念上的問題。特別是,env1不是一個環境,它是一個環境清單。你爲什麼使用map?你在做什麼car的結果?如果您在"let in 5"上運行解釋器(即,沒有可變綁定),會發生什麼情況?

您使用的mapcar建議,我認爲你正在嘗試在自動駕駛儀代碼(「我有一個列表... map確實與列出的東西!」)。無論是或者你認爲extend-env變化(變異)環境和map是一種多次變異的方式。但這是錯誤的。

我的建議:想想你想讓環境包含什麼。爲計算新環境製作一個單獨的輔助函數。使其成爲一個簡單的遞歸函數:沒有像map(還)的更高階的幫助器。爲它編寫測試用例。一旦你得到它的工作(即測試),看看它是否符合你可以使用高階函數來簡化的模式。

+0

非常感謝。這非常有幫助! – CppLearner 2012-03-29 22:13:07

相關問題