更新整个结构 [英] Update the whole structure
问题描述
假设我有一些返回结构的函数:
Suppose I have some function which returns a struct:
(struct layer (points lines areas))
(define (build-new-layer height)
...
(layer list-a list-b list-c))
我想跟踪最后返回的结果,例如:
I want to keep track of the last returned result something like:
(define top-side (build-new-layer 0)) ; store the first result
...
(set! top-side (build-new-layer 0.5)) ; throw away the first result and store the new one
但是,对于该特定代码,我得到了错误:
However, for that particular code I get the error:
set!: assignment disallowed;
cannot modify a constant
constant: top-side
请告诉我什么是做我想要的正确方法
Please, tell me what would be the right way to do what I want
推荐答案
您使用的是哪种语言?看来这是一个配置问题,因为从原则上来说,您所做的应该可以工作.转到选择语言"窗口(在Windows中为Ctrl + L),单击显示详细信息",然后查看当前使用的语言的选项之一是否不允许重新定义变量.或者,尝试使用其他语言.
What language are you using? it seems it's a matter of configuration, because in principle what you're doing should work. Go to the "choose language" window (Ctrl+L in Windows), click on "show details" and see if one of the options of the language currently in use disallows redefinition of variables. Alternatively, try using a different language.
根据 where 的确切含义,您将要使用存储的结果(我无法从问题代码中看出),您可以通过这种方式将其作为函数参数传递不再需要使用全局变量.在Scheme中不建议依赖全局状态和突变(set!
操作),这可能是一个更好的主意.
Depending on where exactly you're going to use the stored result (I can't tell from the code in the question), you could pass it around as function parameters, in such a way that using a global variable is no longer necessary. This might be a better idea, relying on global state and mutation (the set!
operation) is discouraged in Scheme.
这篇关于更新整个结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!