简体   繁体   English

方案-对列表进行排序

[英]Scheme - Sorting a List of Lists

So, I'm working on sorting in Scheme (MIT Scheme). 因此,我正在研究Scheme(MIT Scheme)。 I've figured out how to sort a basic list of integers, however I can't quite seem to figure out how to sort a list of lists. 我已经弄清楚了如何对一个基本的整数列表进行排序,但是我似乎还不太清楚如何对一个列表列表进行排序。 Here's the code for sorting a list of integers via selection sort: 这是通过选择排序对整数列表进行排序的代码:

(define sortdata
    (lambda (lst min)
        (cond ((null? lst) min) ;null list, return current
        ((< (car lst) min) (sortdata (cdr lst) (car lst))) ;compare with head of list 
        (else (sortdata (cdr lst) min))
        )
    )
)

I'm not very well versed in scheme as I've just started using it. 我刚开始使用它时对方案不是很精通。 Rather than sort a list like this one: ( 4 8 9 2 6 15 3), I'm trying to sort a lists of lists, but only by one field the sublist (the age in this case). 而不是像这样的列表排序:(4 8 9 2 6 15 3),我试图对列表列表进行排序,但仅按一个字段对子列表进行排序(在这种情况下为年龄)。 Such as this: (("bob" 17)("mary" 14)) [Assume the first field is the name, and second is the age.] 例如:((“ bob” 17)(“ mary” 14))[假定第一个字段是名称,第二个字段是年龄。]

EDIT: And as such, the sorted list would then look like this: (("mary" 14)("bob" 17)) 编辑:这样,排序列表将如下所示:((“ mary” 14)(“ bob” 17))

Much thanks to anyone who can help or point me to a good reference! 非常感谢任何可以帮助或为我提供良好参考的人!

Introduction 介绍

The basic elements of a search are: 搜索的基本元素是:

  • Table : a collection of records :记录的集合
  • Record : a collection of fields 记录 :字段的集合
  • Field : a value of interest. 字段 :感兴趣的值。

Make a language for your records 记录语言

(define name car)
(define grade cadr)

;; record record -> record
(define (return-record-with-greater-name record1 record2)
  (if (name>? record1 record2)
      record1
      record2))

(define name>?
  (make-record-compare-function string> name))

Make a language for comparing records 制作用于比较记录的语言

;;; This is a partial implementation

;; [any? any? -> boolean] [listof any? -> any?] -> [listof any? -> any?]
(define (make-record-compare-function predicate field)
  (lambda (record1 record2)
    (predicate (field record1) (field record2))))

Make a language for searching tables 编写用于搜索表格的语言

;;; This is a partial implementation

;; [any? any? -> any?] -> [listof any? -> any?]
(define (make-table-search comparator)
  (define  (inner table best-match)
    (if (null? table)
    best-match
    (inner (cdr table)
           (comparator best-match
               (car table)))))
  (lambda (table)
    (inner (cdr table)
           (car table))))

Usage 用法

racket> (define record1 (list "mary" 14))

racket> (define record3 (list "Ben" -42))
racket> (define record2 (list "bob" 17))
racket> (define sort-by-name
                 (make-table-search return-record-with-greater-name))
racket> (sort-by-name (list record3 record1 record2))
'("mary" 14)

Since you are using a comparative sorting algorithm it would be fairly easy to sort other things than integers. 由于您使用的是比较排序算法,因此对整数以外的其他事物进行排序将相当容易。 Basically, what you need to do is parametrize your comparator. 基本上,您需要对比较器进行参数设置。 In your current example this is the function < , however this can be anything! 在您当前的示例中,这是函数< ,但是可以是任何东西!

Note Your code does not work but you are almost there. 注意您的代码不起作用,但您几乎可以使用了。 So I will leave it as is and just show you how you could do it. 因此,我将其保留不变,仅向您展示如何实现。

(define sortdata
    (lambda (lst min comparer)
        (cond ((null? lst) min) ;null list, return current
        ((comparer (car lst) min) (sortdata (cdr lst) (car lst))) ;compare with head of list 
        (else (sortdata (cdr lst) min)))))

In this case your comparar could be: 在这种情况下,您的比较器可能是:

(lambda (x y) (> (cadr x) (cadr y)))

This lambda will take the age of each person ( cadr ) and compare them with eachother. 这个lambda将占用每个人的年龄( cadr )并将它们彼此进行比较。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM