2015/10/06

SICP 問題1.16

反復的べき上プロセスを生成する手続き

(define (fast-expt b n)
  (fast-expt-iter b n 1))

(define (fast-expt-iter b count product)
  (cond ((= count 0) product)
        ((even? count)
         (fast-expt-iter (square b)
                        (/ count 2)
                        product))
        (else (fast-expt-iter b (- count 1) (* b product)))))

© 2022 wat-aro