在 Common Lisp 中查找列表内嵌套最多的列表
我对 Common Lisp 和函数式编程很陌生,但我在 C、C++、C#、Java 等语言方面有很多经验。我无法找到列表中嵌套最多的列表。我的输入是这样的:
(0 1 (2 3) 4 (5 (6 (7) 8)) 9)
我想获得此列表中嵌套最多的列表,在这种情况下,
(7)
我确实有一个想法,可以以某种方式压平列表,直到只剩下一个子列表。为了说明我的意思,这里有几个步骤:
步骤 1. - 输入:
(0 1 (2 3) 4 (5 (6 (7) 8)) 9)
步骤 2. - 展平“第一级”:
(0 1 2 3 4 5 (6 (7) 8) 9)
步骤 3. - 展平“第二级”:
(0 1 2 3 4 5 6 (7) 8 9)
现在只剩下一个嵌套列表,这意味着这是嵌套最多的列表。但我在这里看到一个问题,当出现两个或多个这样的列表时。请分享您对此的想法。
我在 Common Lisp 中将这个过程付诸实践时遇到了问题,所以我将不胜感激一些正确方向的指示,也许是一些示例代码等等。这是一项家庭作业,所以我并不真正期望有一个完整的解决方案,但如果有人指出也许是一个更简单、更好的解决方案及其实现,我会很高兴。
I'm new to Common Lisp and functional programming, but I have a lot of experience in languages like C, C++, C#, Java and so on. I'm having trouble finding the most nested list inside a list. My input is something like this:
(0 1 (2 3) 4 (5 (6 (7) 8)) 9)
I would like to get the most nested list inside this list which in this case is
(7)
I did have an idea that I could flatten the list somehow, until there was only one sub-list left. To illustrate what I mean, here is a few steps:
Step 1. - input:
(0 1 (2 3) 4 (5 (6 (7) 8)) 9)
Step 2. - flatten on "first level":
(0 1 2 3 4 5 (6 (7) 8) 9)
Step 3. - flatten on "second level":
(0 1 2 3 4 5 6 (7) 8 9)
Now there's only one nested list left, which means that was the most nested list. But I see a problem here, when two or more such lists would occur. Please share your thoughts on this.
I'm having problems putting this procedure to reality in Common Lisp, so I would be grateful for some pointers in the right direction, maybe some example code and so on. This is a homework assignment, so I don't really expect a full solution, but would be glad if someone pointed out perhaps an easier and better solution and its implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我在 CL 中的(不是很干净)解决方案:
编辑:
经过再三思考,这是一个稍微干净的解决方案:
Here is my (not very clean) solution in CL:
edit:
After a second thought, this is a slightly cleaner solution:
您迭代地展平列表的方法可能应该可以正常工作,尽管这不是最有效或(主观上)优雅的方法。
如果有多个这样的列表,正确的输出将取决于规范——您应该返回所有列表,还是只返回第一个列表,或者抛出错误?
如果我是你,我会考虑提出一个递归函数来解决这个问题。每个级别的递归基本上都会处理嵌套列表的给定级别的元素。想想每个递归调用应该做什么——如果一旦点击,那就非常简单了!
Your approach of iteratively flattening the list should probably work fine, although it's not the most efficient or (subjectively) elegant way to do it.
If there are more than one such list, the correct output would depend on the specification -- should you return all of them, or just the first one, or throw an error?
If I were you, I'd look in to coming up with a recursive function to solve the problem. Each level of recursion would basically process the elements of a given level of the nested lists. Think of what each recursive call should do -- it's very simple if once it clicks!
这是我的解决方案,使用与OP类似的方法。 (对于多个最深的项目,它们都会被返回。)
我用Scheme编写它,它可能不会立即翻译成Common Lisp,所以我不认为它是一个完整的赠品。不过,它有可能会剧透,所以请谨慎行事。
Here's my solution, using a similar approach to the OP's. (In the case of multiple deepest items, they are all returned.)
I wrote it in Scheme, which probably won't be immediately translatable to Common Lisp, so I don't feel that it'd be a complete giveaway. Still, it has the potential to be spoilery, so tread with caution.