方案 如何从列表中删除元素?
(define (delete atm lis)
(cond
((eq? atm (car lis)) (cdr lis))
(else (cons (car lis) (delete atm (cdr lis))))))
(delete 'a '(b c d a))
(delete 'the '(the more you practice the better you will be))
(delete 'cat '((dog cat) mouse cat (elephant) (cat) cat))
(delete 'rainy '( the weather can be (rainy) sunny cloudy and cold))
我想要的输出是
- (bcd)
- (你练习得越多,你就会越好)
- ((狗猫)老鼠(大象)(猫))
- (天气可能是(雨)晴天阴天和寒冷)
但是有很多错误,请帮助我,谢谢
(define (delete atm lis)
(cond
((eq? atm (car lis)) (cdr lis))
(else (cons (car lis) (delete atm (cdr lis))))))
(delete 'a '(b c d a))
(delete 'the '(the more you practice the better you will be))
(delete 'cat '((dog cat) mouse cat (elephant) (cat) cat))
(delete 'rainy '( the weather can be (rainy) sunny cloudy and cold))
the output I want are
- (b c d)
- (more you practice better you will be)
- ( (dog cat) mouse (elephant) (cat))
- ( the weather can be (rainy) sunny cloudy and cold)
but there are lots of wrong , please help me, thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您实际上并没有删除任何东西。您的过程通常称为
remq
。以下应该有效(未经测试):
You are not actually removing anything. Your procedure is normally known as
remq
.The following should work (untested):
其他两个答案(顺便说一句,它们是相同的)目前仅适用于列表的顶层。如果您还希望它从所有嵌套列表中删除您的原子,您也必须在那里搜索:
如果这不是您想要的,也许您可以指定到底出了什么问题。你一直说某件事或很多事情是错误的,但没有具体说明那是什么。例如,您可以指定您期望四个示例的输出是什么。
The other two answers (which are identical, by the way) currently only work on the top level of the list. If you also want it to remove your atom from all nested lists, you have to search there too:
If this is not what you want, perhaps you can specify what exactly it is that is going wrong. You keep saying that something, or lots of things, are wrong, but not specifying what that is. For instance, you could specify what you expect the output of your four examples to be.
您需要一个基本案例,即使您找到了所需的自动取款机,您仍然希望继续遍历列表。
You need a base case and even when you find the atm you want, you still want to continue recursing through the list.