获取每个列表的最后一个元素

发布于 2024-08-06 07:09:38 字数 105 浏览 4 评论 0原文

假设我有一个列表 ((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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

新雨望断虹 2024-08-13 07:09:38

假设这是关于 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).

野味少女 2024-08-13 07:09:38

与大多数早期的 LISPy 作业问题一样,这也是递归思考和/或归纳思考的练习。开始的方法是问自己一些可以轻松回答的简单问题。

例如,如果您被要求编写一些内容来给出每个列表中的第一个元素,我会这样处理:

给定一个列表列表:

  1. 列表中每个列表的第一个元素是什么 ' ()? (简单 - null
  2. 列表 '(a) 中每个列表的第一个元素是什么? (简单 - a,或者可能是一个错误)
  3. 列表 '((a)) 中每个列表的第一个元素是什么? (简单 - (a)
  4. '(anything) 形式的任何列表的第一个元素是什么,其中任何内容都是列表? (简单 - (first everything)
  5. '(anything morestuff) 形式的每个列表的第一个元素是什么? (easy - (cons (first everything) (first-element morestuff)) )
  6. 原子的第一个是什么?要么是原子,要么是错误(取决于你的观点)
  7. null 的第一个是什么?
  8. 列表中的第一个是什么? (car list)

从这里我们可以开始编写代码:

;; here's first, meeting questions 6-8
(define first (lambda (l)
  (cond 
    ((null? l) nil) ; Q7
    ((atom? l) l)   ; Q6
    (t (car l)))))  ; Q8

;; with first we can write first-element, meeting questions 1-5
(define first-element (lambda (l)
  (cond
    ((null? l) nil) ; Q1
    ((atom? l) (first l))   ; Q2
    (t (cons (first (car l) (first-element (cdr l)))))))) ; Q4-5

现在这不是你的作业(故意的)。您应该尝试一下并了解它是如何工作的。您的下一个目标应该是找出这与您的任务有何不同以及如何实现这一目标。

关于 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:

  1. What is first-element of every list in the list '()? (easy - null)
  2. What is first-element of every list in the list '(a)? (easy - a, or maybe an error)
  3. What is first-element of every list in the list '((a))? (easy - (a))
  4. What is first-element of any list in the form '(anything), where anything is a list? (easy - (first anything))
  5. What is the first element of every list in the form '(anything morestuff)? (easy - (cons (first anything) (first-element morestuff)) )
  6. What is first of an atom? either the atom or an error (depends on your point of view)
  7. What is first of null? nil.
  8. What is first of a list? (car list)

From here we can start writing code:

;; here's first, meeting questions 6-8
(define first (lambda (l)
  (cond 
    ((null? l) nil) ; Q7
    ((atom? l) l)   ; Q6
    (t (car l)))))  ; Q8

;; with first we can write first-element, meeting questions 1-5
(define first-element (lambda (l)
  (cond
    ((null? l) nil) ; Q1
    ((atom? l) (first l))   ; Q2
    (t (cons (first (car l) (first-element (cdr l)))))))) ; Q4-5

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?

つ可否回来 2024-08-13 07:09:38
(defun get-last-lists (s)
    (setq rt 'nil)
    (loop for i from 0 to (- (length s) 1)
        do (setq rt (append rt (last (nth i s)))))
    (print rt))

作为 lisp 的初学者,我发布了我的解决方案。

(defun get-last-lists (s)
    (setq rt 'nil)
    (loop for i from 0 to (- (length s) 1)
        do (setq rt (append rt (last (nth i s)))))
    (print rt))

as a beginner of lisp, i post my solution.

旧城空念 2024-08-13 07:09:38

编写一个返回列表最后一个元素的过程,然后了解一些有关内置 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.

浊酒尽余欢 2024-08-13 07:09:38

也许它已经解决了,但我想出了这个

; SELECT-FROM-INNER-LIST :: [list] -> [list]
(DEFUN SFIL (lst)
  (COND ((NULL lst) NIL)
        ((LISTP (FIRST lst)) (APPEND (LAST (FIRST lst)) (SFIL (REST lst))))
))

现在,这适用于合法列表...所以如果你用正确的列表调用函数 SFIL...如果没有,它将返回 NIL

希望这对任何人都有帮助找到它

probably it is already solved, but I figured this out

; SELECT-FROM-INNER-LIST :: [list] -> [list]
(DEFUN SFIL (lst)
  (COND ((NULL lst) NIL)
        ((LISTP (FIRST lst)) (APPEND (LAST (FIRST lst)) (SFIL (REST lst))))
))

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文