根据某些元素对列表进行排序

发布于 2024-10-10 08:13:11 字数 366 浏览 0 评论 0原文

我是 Lisp 的新手,我想学习 Lisp 编程。 我想对从文本文件读取的一些列表进行排序,如下所示:

(a 120 135 124 124)
(b 120 135 124 124)
(c 120 135 124 124)

根据第一个整数元素或第二个或第三个整数元素对它们进行排序的最佳方法是什么?

我有以下想法:

  1. 将它们全部读取并将它们放入列表列表中,
  2. 迭代容器列表并将列表的值与下面的值进行比较,就像冒泡排序一样。

是否有更合适的数据结构来实现这一点,也许像Java中的集合一样,它采用包含排序逻辑和自动完成排序的可比对象?

非常感谢。

I am a newbie in Lisp and I want to learn Lisp programming.
I want to sort some lists read from a text file, like in the following form:

(a 120 135 124 124)
(b 120 135 124 124)
(c 120 135 124 124)

What is the best way to sort them according to the first integer element or maybe second or third and so on?

I have the following idea:

  1. read them all and put them into a list of lists
  2. iterate over the container list and compare the values of list with following one like in bubble sort.

Are there more suitable data structures to achieve this, maybe like Collections in Java which take comparable objects that contain sort logic and fullfill sorting automatically?

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

究竟谁懂我的在乎 2024-10-17 08:13:11

标准 sort 函数采用 :key 参数,该参数可用于从对象中提取值以用作排序键。对于您的示例,如果文件中的每个列表都位于名为 objects 的列表中,则以下代码将按第一个整数元素对 objects 进行破坏性排序,并返回排序后的列表:

(sort objects #'< :key #'second)

请参阅http://l1sp.org/cl/sort 了解 Common Lisp 的 sort 函数的精确规范。

The standard sort function takes a :key argument that can be used to extract a value from the object to use as the sort key. For your example, if you had each list from the file in a list called objects, the following would destructively sort objects by the first integer element and return a sorted list:

(sort objects #'< :key #'second)

See http://l1sp.org/cl/sort for the precise specification of Common Lisp's sort function.

疧_╮線 2024-10-17 08:13:11
(defun position-of-first-int (alist)
  (position (find-if 
             #'(lambda (x) (not (numberp x)))
             alist)
            alist))

(defun sort-from-first-int (alist)
  (sort (subseq alist (1+ (position-of-first-int alist))) #'<))

测试:

> (setf a '(a 120 135 124 124))
> (setf b '(120 b 135 124 124))
> (setf c '(120 135 c 124 110))

> (format t "~a~%" (sort-from-first-int a))
(120 124 124 135)
> (format t "~a~%" (sort-from-first-int b))
(124 124 135)
> (format t "~a~%" (sort-from-first-int c))
(110 124)
(defun position-of-first-int (alist)
  (position (find-if 
             #'(lambda (x) (not (numberp x)))
             alist)
            alist))

(defun sort-from-first-int (alist)
  (sort (subseq alist (1+ (position-of-first-int alist))) #'<))

Test:

> (setf a '(a 120 135 124 124))
> (setf b '(120 b 135 124 124))
> (setf c '(120 135 c 124 110))

> (format t "~a~%" (sort-from-first-int a))
(120 124 124 135)
> (format t "~a~%" (sort-from-first-int b))
(124 124 135)
> (format t "~a~%" (sort-from-first-int c))
(110 124)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文