“设置!:不是标识符:...” plt方案错误
Dr.Scheme 中使用 Pretty Big 的这段代码有什么问题?我似乎记得过去做过类似的事情,没有任何问题。
(lambda (x y) (set! (Frame-variables res) (append (Frame-variables res) (list (cons x y)))))
返回以下错误:
set!: 不是标识符:(Frame-variables res)
如果我省略 (set!(Frame-variables res) ... )
部分,它可以正常工作,除了当然,我的清单实际上并没有改变。所以它识别第二个 (Frame-varialbes res)
但不能识别第一个?或者在这种情况下 set!
有什么用处?
希望您认识到我有一个名为 Frame
的结构,其中有一个 variables
字段,它是一个列表。
what's wrong with this code in Dr.Scheme using Pretty Big? i seem to remember doing similar things in the past with no problem.
(lambda (x y) (set! (Frame-variables res) (append (Frame-variables res) (list (cons x y)))))
which returns the following error:
set!: not an identifier in: (Frame-variables res)
if i omit the (set! (Frame-variables res) ... )
part it works fine except of course that my list doesn't actually change. so it recognizes the second (Frame-varialbes res)
but not the first one? or what's the deal with set!
in that context?
hopefully you recognize that i have a struct called Frame
with a variables
field which is a list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题:
如何设置!作品。它真正做的是将一个名称绑定到一个挂在内存中某处的对象。当您设置!时,它会更改该名称所指的内容。
这就是错误:
(Frame-variables res)
不是标识符,因此它不能使其引用其他内容。修复:
您想要做的是改变结构体的成员。您需要做的是将结构成员更改为可变的,然后使用将要创建的变异器。
然后,您可以使用
这将根据需要改变您的结构。
讲座:
我想知道您是否为您的程序选择了一个好的设计。变异和附加到列表这两种操作都让我对Scheme 中的设计产生疑问。
另外,看起来您正在做的事情是尝试根据您对变量的调用来维护变量的关联列表。这并不是真正有效,也许您应该考虑其他东西,例如 哈希表
相关文档:
http://docs.plt-scheme.org/reference/define-struct.html#(form._((lib._scheme/base..ss )._define-struct))
The problem:
How set! works. What it really does is bind a name to an object that is hanging around in memory somewhere. When you do set!, it changes what that name refers to.
That's the error:
(Frame-variables res)
isn't an identifier, so it can't make it refer to something else.The fix:
What you're trying to do is mutate a member of a struct. What you need to do is change that structure member to be mutable, and then use the mutator that will be made.
Then, you can use
This will mutate your structure as desired.
The Lecture:
I'm wondering if you're choosing a good design for your program. Mutating and appending to a list are both operations that make me question a design in Scheme.
Also, it looks like what you're doing is trying to maintain an association list of variables, based on what you've called them. This isn't really efficient, and perhaps you should consider something else like a Hash Table
Relevant documentation:
http://docs.plt-scheme.org/reference/define-struct.html#(form._((lib._scheme/base..ss)._define-struct))