方案排序列表不同的标准 [英] scheme sort list diffent criteria

查看:23
本文介绍了方案排序列表不同的标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有限的四元组列表,例如

I have a finite list of quadruples, e.g.

(list (list 1 3 5 5) (list 2 3 4 9) (list 3 4 4 6)(list 4 7 10 3)).

我用 (a1 a2 a3 a4) 表示每个元素.

I denote each of the elements by (a1 a2 a3 a4).

请帮我写一个排序函数,它提供根据以下标准创建的递增"列表:

Please help me to write a sorting function which provides a "increasing" list created according to the following criteria:

  1. 数字a2,
  2. 后来的差异(a3 - a4),
  3. 然后是数字 a3.

如果可以,请帮忙.

推荐答案

据我所知,您的排序条件就是排序的顺序.如果是这种情况,则以下程序应执行该排序.

As far as I can tell, your ordered criteria are the order in which to sort. If this is the case, then the following program should perform that sorting.

(define (strange-sort quadruples)
  (define (a2 quad)
    (cadr quad))
  (define (a3 quad)
    (caddr quad))
  (define (a4 quad)
    (cadddr quad))
  (sort quadruples
        (lambda (x y)
          (cond ((< (a2 x) (a2 y))
                 #t)
                ((> (a2 x) (a2 y))
                 #f)
                (else
                 (cond ((< (- (a3 x) (a4 x))
                           (- (a3 y) (a4 y)))
                        #t)
                       ((> (- (a3 x) (a4 x))
                           (- (a3 y) (a4 y)))
                        #f)
                       (else
                        (cond ((< (a3 x) (a3 y))
                               #t)
                              (else #f)))))))))

这篇关于方案排序列表不同的标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆