删除第一个元素的方案函数
编写一个方案函数,删除给定项目的第一个顶级出现 从项目列表中。 例如给定列表(abc)项目b,结果列表(ac)
请帮助我
write a scheme function that remove the first top level occurrence of a given item
from a list of items.
eg given list (a b c) item b, result list (a c)
plz help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
想想你想实现什么目标。
您有一个内容列表,并且正在尝试删除某个元素。
如果我们将其简化为递归操作:
Think what are you trying to accomplish.
You have a list of stuff and you are trying to remove a certain element.
if we were to reduce this to operating with recursion:
不确定你想要什么,但首先简单地从一个索引开始,这几乎就是你必须用Scheme“思考”的方式,首先从“如果它是第一个元素怎么办?”开始,答案当然是它应该是列表的其余部分。然后“好吧,如果它不是第一个怎么办”,那么答案是“它应该限制第一个,直到将相同过程应用于其余部分的结果。”,这就是方案在这方面需要的所有信息,并且在很多情况下真的。
如果列表对于索引来说太短,则此函数将返回错误。
然而,如果你想通过与另一个对象的某种相等性来切片,这个例子就足够了,我们现在不再将它切片到我们达到 0 的地方,但如果它与搜索示例相同:
使用完全相同的原理,但是这个会返回如果未找到该项目,则会出现错误。
重点是,scheme 有多种形式的相等比较,所以,我们真正想要的是这样的:
这个例子展示了Scheme 的全部内容,不确定你是否了解它,但我们在这里使用了一个闭包,这个函数实际上将任何二进制比较函数作为参数,然后计算出您想要的函数,它也被清理了,如果找不到它,它不会再犯错误,它返回它只是返回旧列表,因为如果它到达末尾列表,还没有删除任何内容,它只是再次将其精简为 () 。
但是记住我们原来的函数,我们现在可以在提供谓词“等于?”时像这样简单地定义它。我们的新函数实际上评估我们的旧函数(重要的是它现在已经被清理):
而且,还有更多的二进制比较,这个更奇特的例子怎么样:
我们用这种方式创建了一个函数,它切掉第一个严格小于的元素比我们的搜索词大,因此这是有效的:
尽管 47 也小于 100,但 92 是其中的第一个。
Not sure what you want, but first start it simply with an index, this is pretty much how you have to 'think' with Scheme, first start with 'What if it were the first element?', the answer then is of course that it should be the rest of the list. And then 'Well, what if it's not the first', then the answer is 'It should cons the first up to the result of the same procedure applied to the rest.', that's all the info Scheme needs in this, and many cases really.
This function returns an error if the list is too short for the index.
However, if you want to slice out by some equality to another object, this example suffices, we now no longer slice it out of we reach 0, but if it's identical to a search example:
Using the very same principles, This one however returns an error if the item isn't found.
Point is that scheme has many flavours of equality comparisons, so, what we actually want is this:
This example shows what Scheme is all about, not sure if you're known with it, but we've used a closure here, this function actually takes as argument any binary comparison function and then evaluate to the function you want, it's also sanitized, it doesn't make an error any more if it's not found, it returns it simply returns the old list because if it reaches the end of the list, with nothing removed yet, it just conses it up to () again.
But remembering our original function, we can now define it simply like this, when supplying the predicate 'equal?' our new function actually evaluates to our old function (with the important asset that it's now sanitized):
And, there's more binary comparison, how about this more exotic example:
We made a function this way that slices out the first element that is strictly less than our search term, thus this works:
Even though 47 is also less than 100, 92 is the first of those that is.
有那些功能 car 和 cdr 允许您获取列表的一部分。函数append 允许您将两个列表合并为一个。我想它们可能会派上用场。还要检查 cons 函数并确保您了解列表实际上是什么以及它与对有什么关系。
例如,您可以按如下方式进行。拿出你的清单,剪下第一个元素并检查它是什么。如果是要删除的,请将其丢弃。如果它不是要删除的元素,请处理列表的其余部分,然后将该元素附加回开头。
There are those function car and cdr that allow You to take parts of the list. Function append allows You to joint two lists into one. I think they might come handy. Also check out the cons function and make sure You understand what a list actually is and what does it have to do with pairs.
You can proceed as follows for example. Take Your list, cut the first element out and check what it is. If it's the one to be removed discard it. If it's not the element to be removed, process the rest of the list and then append that element back at the beginning.
像这样的东西(如果它是作业):
Something like this (if it is a homework):
(定义(删除首次出现列表元素累加)
(条件
((空?列表)累加)
(别的
(cond((= (汽车列表) 元素) (cons Accum (cdr 列表)))
(else(删除首次出现(cdr列表)元素(cons(汽车列表)accum)))
)
)
)
)
(删除首次出现 '(1 2 3) 2 '())
(define (remove-first-occurence list element accum)
(cond
((null? list) accum)
(else
(cond((= (car list) element) (cons accum (cdr list)))
(else(remove-first-occurence (cdr list) element(cons (car list) accum)))
)
)
)
)
(remove-first-occurence '(1 2 3) 2 '())
实现 perl2scheme 的剩余琐碎任务留给读者作为练习。
The trivial remaining task of implementing perl2scheme is left as an excercize for the reader.