(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)))))
如何使用自我组合排序步骤进行排序? 尝试:
(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
答案 0 :(得分:1)
(sort-step l f)
不是compose
所期望的一个参数的函数,它是一个列表。
因为你可能想要"线程"通过合成列表,您需要编写一个获取列表并返回列表的函数。
你可以通过将sort-step
稍微重新排列成一个curried函数来获得一个:
(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))
答案 1 :(得分:0)
l param也可以在自编写中通过lambda线程而不重写sort-step:
(define (sort-f l f)
((self-compose (lambda (l) (sort-step l f)) (length l)) l))