获取每个列表的最后一个元素
假设我有一个列表 ((3 4 5) (def) (hij) (5 5 5 5))
如何以输出的方式获取每个列表的最后一个元素看起来像这样(5 fj 5)?
Let us say I have a list ((3 4 5) (d e f) (h i j) (5 5 5 5))
How can I get the last element of each list in such a way that the output would look like this (5 f j 5)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设这是关于 Common Lisp 的,有一个函数 last 返回一个包含列表最后一项的列表。如果将此函数与 mapcan 一起使用,它将给定函数应用于每个列表的元素并返回连接的结果,您将得到您想要的。
请注意,虽然访问列表的最后一个元素是一个
O(N)
操作,所以如果这毕竟不仅仅是家庭作业,您可能需要考虑是否无法解决真正的问题比获取每个列表的最后一项更有效(也许使用另一个数据结构代替)。Assuming this is about Common Lisp, there is a function last which returns a list containing the last item of a list. If you use this function with mapcan, which applies a given function to each element of a list and returns the concatenated results, you'll get what you want.
Note though that accessing the last element of a list is an
O(N)
operation, so if this isn't just homework after all, you might want to consider if you can't solve the real problem more efficiently than taking the last item of each list (maybe use another datastructure instead).与大多数早期的 LISPy 作业问题一样,这也是递归思考和/或归纳思考的练习。开始的方法是问自己一些可以轻松回答的简单问题。
例如,如果您被要求编写一些内容来给出每个列表中的第一个元素,我会这样处理:
给定一个列表列表:
' ()? (简单 -
null
)'(a)
中每个列表的第一个元素是什么? (简单 -a
,或者可能是一个错误)'((a))
中每个列表的第一个元素是什么? (简单 -(a)
)'(anything)
形式的任何列表的第一个元素是什么,其中任何内容都是列表? (简单 -(first everything)
)'(anything morestuff)
形式的每个列表的第一个元素是什么? (easy -(cons (first everything) (first-element morestuff))
)无
。(car list)
从这里我们可以开始编写代码:
现在这不是你的作业(故意的)。您应该尝试一下并了解它是如何工作的。您的下一个目标应该是找出这与您的任务有何不同以及如何实现这一目标。
关于 MAPCAR?别担心。您需要首先学习如何解决递归问题。那么您就可以担心 MAPCAR 了。这个任务的意义何在?帮助您学会以这种模式思考。 LISP/Scheme 中的所有几乎都是通过这种方式思考来解决的。
我之所以把所有问题分解成我担心的部分。如果给我一个任务“如何对列表中的每个项目执行 foo 操作?”我应该回答以下问题:我如何处理 null?如何处理原子?如何处理列表中的第一个元素?我该如何处理其他一切?一旦我回答了这个问题,我就会弄清楚如何实际执行 foo.如何在 null 上执行 foo 操作?如何在原子上执行 foo 操作?如何在列表中执行 foo 操作?
This, like most early LISPy homework problems is an exercise in thinking recursively and/or thinking in terms of induction. The way to start is to ask yourself simple questions that you can answer easily.
For example, if you had been asked to write something that gave you the first element in each list, I would thing about it this way:
Given a list of lists:
'()
? (easy -null
)'(a)
? (easy -a
, or maybe an error)'((a))
? (easy -(a)
)'(anything)
, where anything is a list? (easy -(first anything)
)'(anything morestuff)
? (easy -(cons (first anything) (first-element morestuff))
)nil
.(car list)
From here we can start writing code:
Now this isn't your homework (intentionally). You should play with this and understand how it works. Your next goal should be to find out how this differs from your assignment and how to get there.
With respect to MAPCAR? Don't worry about it. You need to learn how to solve recursive problems first. Then you can worry about MAPCAR. What is the point of this assignment? To help you learn to think in this mode. Dang near everything in LISP/Scheme is solved by thinking this way.
The reason I went with all the questions to break it down into the parts that I'm worried about. If I'm given the task "how do I do foo on every item in a list?" I should answer the questions: How do I do handle null? How do handle an atom? How do I do handle on the first element on the list? How do I handle everything else? Once I've answered that, then I figure out how to actually do foo. How do I do foo on null? How do I do foo on an atom? How do I do foo on a list?
作为 lisp 的初学者,我发布了我的解决方案。
as a beginner of lisp, i post my solution.
编写一个返回列表最后一个元素的过程,然后了解一些有关内置
MAP
(又名MAPCAR
)过程的知识,并查看是否有灯泡熄灭。Write a procedure that returns the last element of a list, then learn a little about the built-in
MAP
(a.k.a.MAPCAR
) procedure and see if any lightbulbs go off.也许它已经解决了,但我想出了这个
现在,这适用于合法列表...所以如果你用正确的列表调用函数 SFIL...如果没有,它将返回 NIL
希望这对任何人都有帮助找到它
probably it is already solved, but I figured this out
Now, this works for legit list...so if you call function SFIL with correct list.... if not, it will return NIL
hopefully this will be helpful, for anyone who finds it