scheme sort list diffent criteria

前端 未结 1 782
醉酒成梦
醉酒成梦 2021-01-25 22:11

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)).

I denote each of the elements by

1条回答
  •  伪装坚强ぢ
    2021-01-25 22:41

    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)
        (caffffdr 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)))))))))
    

    0 讨论(0)
提交回复
热议问题