SICP Section 1.2.4
The code below is exercises 1.16 and 1.17. I’ll finish up 1.18 and 1.19 some time in the future. I’ve created a repository on github to store the code I write while working through SICP – if you’re interested it can be found at
http://github.com/mbowcock/SICP.
Exercise 1.16 -
(define (square n) (* n n)) (define (fast-expt-iter b n a) (cond ((= n 0) a) ((even? n) (fast-expt-iter b (- n 2) (* a (square b)))) (else (fast-expt-iter b (- n 1) (* a b))))) (define (fast-expt-new b n) (fast-expt-iter b n 1))
Exercise 1.17 –
(define (double n) (* n 2)) (define (halve n) (/ n 2)) (define (*. a b) (cond ((= b 0) 0) ((even? b) (*. (double a) (halve b))) (else (+ a (*. a (- b 1))))))