“设置!:不是标识符:...”plt scheme错误

时间:2009-12-10 04:53:17

标签: scheme racket mutability

Dr.Scheme使用Pretty Big的代码有什么问题?我似乎记得在过去做过类似的事情没有问题。

(lambda (x y) (set! (Frame-variables res) (append (Frame-variables res) (list (cons x y)))))

返回以下错误:

  

set!: not an identifier in: (Frame-variables res)

如果我省略(set! (Frame-variables res) ... )部分它工作正常,当然我的列表实际上没有改变。所以它识别第二个(Frame-varialbes res)而不是第一个set!?或者在这种情况下与Frame的交易是什么?

希望您认识到我有一个名为variables的结构,其中{{1}}字段是一个列表。

1 个答案:

答案 0 :(得分:4)

问题:

怎么设定!作品。它真正做的是将名称绑定到某个地方的内存中的对象。当您设置!时,它会更改该名称所指的内容。

这就是错误:(Frame-variables res)不是标识符,因此无法使其引用其他内容。

修复:

您要做的是改变结构的成员。你需要做的是将结构成员改为可变,然后使用将要制作的mutator。

(define-struct frame (a b ... c [variables #:mutable] e ...))

然后,您可以使用

(set-frame-variables! res (list "new" "value"))

这会根据需要改变你的结构。

讲座:

我想知道你是否为你的程序选择了一个好的设计。变异和附加到列表都是使我质疑Scheme中的设计的操作。

此外,您正在尝试根据您所称的变量维护变量关联列表。这不是很有效,也许你应该考虑像Hash Table

这样的东西

相关文档:

http://docs.plt-scheme.org/reference/define-struct.html#(form._((lib._scheme/base..ss)._define-struct))