; Scheme and the Art of Programming
; Chapter 7

; -- Exercise 7.1
; What operand do we pass to k to get the same value as (h 8)?

(define (add1 n) (+ n 1))
(define (compose f g) (lambda (x) (f (g x))))

(define h (compose sqrt add1))
(define k (compose add1 sqrt))

(h 8)
; Value: 3
(k 4)
; Value: 3


; -- Exercise 7.2
; Use the procedure compose to define a procedure compose3 
; that takes as arguments three procedures, f, g and h, and 
; returns the composition k such that for each argument 
; x, k(x) = f(g(h(x))).

(define (compose3 f g h) (lambda (x) (f (g (h x)))))


; -- Exercise 7.3
; Use the unrestricted lambda to define a composition procedure 
; compose-many that forms the composition of arbitrarily many 
; procedures of one argument.

(define compose-many
  (lambda funcs 
    (lambda (x)
      (if (null? (cdr funcs)) ((car funcs) x)
	  ((car funcs) (compose-many (cdr funcs)))))))
