在Scheme中的List中添加一个元素
下面是我的代码,它采用列表(carVal)
的 car 元素和列表(初始化为空)作为参数。我想将元素追加到列表中,但同样不起作用。
(define populateValues
(lambda (carVal currVal)
(append currVal(list carVal ))
(display currVal)))
显示屏始终显示空列表 ()
。谁能帮我理解为什么?
Below is my code which takes a car element of a list(carVal)
and an list(initialized to empty) as parameters. I want to append the element to the list but the same is not working.
(define populateValues
(lambda (carVal currVal)
(append currVal(list carVal ))
(display currVal)))
The display shows empty list all the time ()
. Can anyone help me understand why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,有
append!
作为一个原语,它解决了你的大部分问题,正如已经指出的,Scheme 倾向于不赞成突变,这是可能的,但通常会避免,所以所有突变的过程都有一个!
(称为“爆炸”)在其末尾。另外,
set!
不会改变数据,它会改变环境,它使变量指向另一个事物,原始数据保持不变。在Scheme中改变数据是相当麻烦的,但是,给你我自己的append实现!看看它是如何完成的:
注意 set-cdr! 的使用,它是一个真正的变异器,它只对成对起作用,它会改变内存中的数据,与“set!”不同。如果将一对传递给函数并使用 set-cdr 进行变异!或 set-car!,它在程序中的每个地方都发生了变化。
这遵循 SRFI 附录!例如,规范规定它应该是可变的,并且应该返回一个未定义的值。
显示:
如可见,追加!可以接受无限多个参数,并且它会改变除最后一个之外的所有参数。
不过,Scheme 可能不是您的理想语言。使用追加!正如之前所说,这是非标准的,相反,append 是首选,它不会发生变化,并且会被调用以获取其返回值。我是这样实现的:
在没有突变、大量使用递归的情况下,它显示了更熟悉的方案风格
并且不使用测序。
编辑:如果您只想将一些元素添加到列表中,而不是本身加入两个元素:
这符合您的期望
Well, there is
append!
as a primitive, which solves most of your problems, as noted already, Scheme tends to frown on mutation, it is possible, but typically avoided, so all procedures that mutate have a!
(called a bang) at their end.Also,
set!
does not mutate data, it changes an environment, it makes a variable point to another thing, the original data is left unchanged.Mutating data in Scheme is quite cumbersome, but, to give you my own implementation of append! to see how it is done:
Note the use of
set-cdr!
, which is a true mutator, it only works on pairs, it mutates data in memory, unlike `set!'. If a pair is passed to a function and mutated with set-cdr! or set-car!, it is mutated every-where in the program.This obeys the SRFI append! spec which says that it should be variadic and that it should return an undefined value, for instance.
Which displays:
As visible, append! can take an infinite number of arguments and it mutates them all but the last.
Scheme might not be the ideal language for you though. The use of append! as said before is nonstandard, instead, append is preferred, which does not mutate and is called for its return value. Which I implement as such:
Which shows a more familiar Scheme style in the absence of mutation, heavy use of recursion
and no use of sequencing.
Edit: If you just want to add some elements to a list and not per se join two though:
Which does what you expect
append
创建一个新列表,它不会修改现有列表。set!
来更接近——但即使这样也会让您失望,因为它只会修改本地绑定。append
creates a new list, it does not modify an existing one.set!
-- but even that will disappoint you since it will modify only the local binding.(append foo bar)
返回foo
和bar
的串联。它不会更改foo
或bar
。(append foo bar)
returns the concatenation offoo
andbar
. It doesn't change eitherfoo
orbar
.您必须使用 set! 更新 currVal 的值。你的例子应该有
You have to update the value of currVal with set!. Your example should have
您确实需要考虑您正在寻找的确切功能。
如果您想就地改变引用列表,那么您必须执行相当于追加的操作! (如其他答案中所述)。但这是危险的,因为您可能有其他代码依赖于列表是不可变的,如果您打算这样做,您的过程需要有一个!最终标记出这种危险。
以更实用的方式,对您想要做的事情的一个简单的近似是:
请注意,它会创建一个新列表,进行追加,显示结果,然后将新列表作为值返回。如果您无权访问中间值,这是一种有用的调试技术:绑定到变量,显示或记录它,然后返回它。
You really need to think about what exact functionality you are looking for
If you want to mutate a referenced list in place, then you have to do the equivalent of append! (as noted in the other answers). But that is dangerous, BECAUSE you may have other code that is counting on the list being immutable, and if you are going to do that, your procedure needs to have a ! in the end to flag that danger.
A cheap approximation to what you want to do, in a more functional style, is:
Note that it makes a new list, does the append, displays the result, and RETURNS the new list as a value. This is a useful debugging technique if you don't have access to the intermediate value: bind to a varible, display or log it, and then return it.