方案深度逆向函数
我正在做一个方案程序,它接受一个列表,然后反转它。到目前为止,它适用于不包含任何子列表的简单列表,但是当我测试它的列表是否包含子列表时,它失败了。请帮我看看哪里出了问题。 这是代码:
(define deep-reverse
(lambda (L)
(cond
((empty? L) '())
(else (append (deep-reverse (rest L)) (list (first L)))))))
I am doing a scheme program which takes in a list and then reverse it. So far it works for simple list, which does not contain any sublist, but when I test it for a list contains sublist, it fails. Please help me where is wrong with it.
Here is the code:
(define deep-reverse
(lambda (L)
(cond
((empty? L) '())
(else (append (deep-reverse (rest L)) (list (first L)))))))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您正在使用未定义的Scheme 函数。我将解决以下假设:
空?为空?
其余是 cdr
首先是汽车
您的代码的工作原理是获取列表中的第一个元素并将其添加到另一个列表中。但是,列表的第一个元素可以是列表本身。您需要测试看看您正在处理的元素是原子元素还是列表。如果它是一个列表,那么您递归地调用深度反转。
如果您想查看附加的代码,请发表评论。
First of all, you are using undefined Scheme functions. I am going to work off the following assumptions:
empty? is null?
rest is cdr
first is car
Your code works by taking the first element in a list and adding it to another list. However, that first element of a list can be a list itself. You need to test to see if the element that you're working on is atomic or a list. If it's a list, then you call deep-reverse recursively.
If you would like to see code appended to this, leave a comment.