Daniel-Nikpayuk_SICP-Solutions/Chapter 1/1.3/exercise-1.43 (fast-composition)/exercise.lsp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Copyright 2015 Daniel Nikpayuk
;;
;; This file is part of SICP Solutions.
;;
;; SICP Solutions is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
;;
;; SICP Solutions is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License along with SICP Solutions. If not, see
;; <http://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (identity x) x)
(define (square x) (* x x))

(define (compose f g)
 (lambda (x) (f (g x)))
)

(define (compose-square f)
 (lambda (x) (f (f x)))
)

;fast composition.

(define (repeated f n)
 (define (fast prim sec n)
  (cond
   ((= n 0) prim)
   ((even? n) (fast prim (compose-square sec) (/ n 2)))
   (else (fast (compose prim sec) sec (- n 1)))
  )
 )
 (fast identity f n)
)

(display "1.43")
(newline)
(display ((repeated square 2) 5))

