删除列表的最后一个元素(方案)
所以我必须删除方案中列表的最后一个元素。
例如,假设我有一个列表 (1 2 3 4)
。我需要返回:
(1 2 3)
我的想法:
reverse(list)
car(list)
reverse(list)
scheme(racket)中有reverse
函数吗?
So I have to remove the last element of a list in scheme.
For example, let's say I have a list (1 2 3 4)
. I need to return:
(1 2 3)
My idea:
reverse(list)
car(list)
reverse(list)
Is there a reverse
function in scheme(racket)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你写道:“倒车,汽车,倒车”。我相信你的意思是写“反向,cdr,反向”。这个解决方案没有任何问题;它与列表的大小成线性关系,就像使用标准列表的任何解决方案一样。
作为代码:
如果列表的多次遍历或不必要的另一个列表副本的构造让您烦恼,您当然可以通过直接编写来避免它。
鉴于您的几乎解决方案,我将假设这不是家庭作业。
在球拍中,它看起来像这样:
You wrote: "reverse, car, reverse". I believe you meant to write "reverse, cdr, reverse". There's nothing wrong with this solution; it's linear in the size of the list, just like any solution to this that uses the standard lists.
As code:
If the multiple traversal of the list or the needless construction of another list copy bothers you, you can certainly avoid it, by writing the thing directly.
Given your almost-solution, I'm going to assume that this isn't homework.
Here's what it would look like, in racket:
SRFI 1(使用
在 Racket 中激活(需要 srfi/1 )
) 有一个drop-right
函数:SRFI 1 (activate in Racket using
(require srfi/1)
) has adrop-right
function:有一个
reverse
,但是使用它不会很有效。我建议使用以下递归函数。if
检查它是否位于列表的最后一个元素。There is a
reverse
, but using it would not be very efficient. I suggest the following recursive function.The
if
checks whether it is at the last element of the list.我会执行一个递归函数,如果后面的元素不是最后一个元素,则沿着列表向下移动并附加该元素(使用 cons),如果不是最后一个元素,则不附加任何内容。
我已经很多年没有做过计划了,所以这就是我能做的。
有人可以运行如何实现它(除非这是家庭作业,否则他们可能不应该!)
I would do a recursive function that goes down the list and attaches the element (using
cons
) if the element after it is not the last, and appends nothing if it isn't.I haven't done scheme for years though so that's as far as I can go.
Someone can run with how to implement it (unless it's homework then they probably shouldn't!)
我做了一些比以下更简单的事情:reverse(list),car(list),reverse(list)来获取最后一个元素,请查看:
I've done something simpler than: reverse(list), car(list), reverse(list) to get the last element, check out:
正在寻找其他方法的人可以检查一下:
Those who are looking for another way can check this out:
我会编写一个简单的递归,将典型的“empty?mylist”基本情况更改为“empty?(rest mylist)”,这样当输入列表只有 1 个元素时我可以返回空。
顺便说一句,这段代码位于 Racket/PLT Scheme 中,它是 Scheme 的子集。
I would write a simple recursion, altering the typical "empty? mylist" base case to "empty? (rest mylist)," so that I can return empty when the input list is only 1 element.
By the way, this code is in Racket/PLT Scheme, a subset of Scheme.
(已编辑:名称已更改;在空和单元素列表上发出错误信号)
方案列表是可变的,因此可以删除多元素列表的最后一个元素:
可以在 DrRacket 中尝试此方案代码,方法是在其前面添加前缀:
For
#langracket
,可以尝试将set-cdr!
替换为不安全版本。
(Edited: name changed; signals error on null and single element lists)
Scheme lists are mutable, so the last element of a multi-element list can be removed:
This Scheme code can be tried in DrRacket by prefixing it with:
For
#lang racket
, one could try replacingset-cdr!
withan unsafe version.