some solutions for MIT Press's "structure and interpretation of computer programs", I guess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
334 B

(define (square x)
(* x x))
(define (nice-expt b n)
(define (iter a b n)
(cond ((zero? n) a)
((even? n) (iter a (square b) (/ n 2)))
(else (iter (* a b) b (- n 1)))))
(iter 1 b n))
(display (nice-expt 3 2))
(newline)
;; (nice-expt 3 2)
;; -> (iter 1 3 2)
;; -> (iter 1 9 1)
;; -> (iter 9 9 0)
;; => 9