Generalized compose

(define (self-compose* f n) ; Return a function that applies f n times on its parameter. n >= 1. (cond ((= n 1) f) ((> n 1) (lambda (x) (f ((self-compose* f (- n 1)) x)))) (else (error "self-compose-n: n must be an integer larger than 0")))) (define (compose* f-list) ; Return a function that applies all functions to each other sequentially. The first function in f-list is applied as the last one. (cond ((= (length f-list) 1) (car f-list)) ((> (length f-list) 1) (lambda (x) ((car f-list) ((compose* (cdr f-list)) x)))) (else (error "compose* must be applied on a non-empty function list")))) ; Alternative, and more attractive variant: (define (compose* fn-lst) (cond ((= (length fn-lst) 1) (car fn-lst)) ((> (length fn-lst) 1) (compose (car fn-lst) (compose* (cdr fn-lst)))) (else (error "compose* must be applied on a non-empty function list")))) ; An alternative implementation of compose* in terms of compose and reduce-right: (define f (reduce-right compose (list incr - fac))) (define compose* ((curry2 reduce-right) compose)) (define (incr n) (+ n 1)) (define (fac n) (if (= n 0) 1 (* n (fac (- n 1)))))