如何使用 self-compose with sort-step 进行排序

How to use self-compose with sort-step to sort

(define (compose f1 f2)
  (lambda (p2) (f1 (f2 p2))))

(define (self-compose f n)
  (if (= n 1) (compose f f)
      (compose f (self-compose f (- n 1)))))

(define (sort-step l f)
  (cond ((eq? l '()) '())
        ((eq? (cdr l) '()) (list (car l)))
        ((f (car l) (cadr l)) (cons (car l) (sort-step (cdr l) f)))
        (else (cons (cadr l) (sort-step (cons (car l) (cddr l)) f)))))

如何使用self-compose with sort-step进行排序? 尝试过:

(define (sort-f l f)
  (self-compose (sort-step l f) (length l)))

测试:

(sort-f '(8 4 6 5 3) >)  ===> arity mismatch;
the expected number of arguments does not match the given number
  expected: 1
  given: 0

(sort-step l f) 不是 compose 期望的一个参数的函数,它是一个列表。

由于您可能希望通过组合 "thread" 列表,因此您需要组合一个接受列表和 returns 列表的函数。

你可以通过将 sort-step 稍微重新排列成柯里化函数来获得一个:

(define (sort-step f)
  (lambda (l)
    (cond ((null? l) '())
          ((null? (cdr l)) l)
          ((f (car l) (cadr l)) (cons (car l) ((sort-step f) (cdr l))))
          (else (cons (cadr l) ((sort-step f) (cons (car l) (cddr l))))))))

现在 (sort-step f) 是一个从列表到列表的函数,你可以说

(define (sort-f l f)
  ((self-compose (sort-step f) (length l)) l))

l参数也可以在self-compose中被lambda线程化而无需重写sort-step:

(define (sort-f l f) 
  ((self-compose (lambda (l) (sort-step l f)) (length l)) l))