hw3 - ; #1. ; Exercise 1.16 (define (invariant-fast-expt b...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
; #1. ; ;; Exercise 1.16 (define (invariant-fast-expt b n) (cond ((= n 0) 1) ((even? n) (* (/ 1 (invariant-fast-expt (square b) (/ n 2))) (invariant-fast-expt (square b) (/ n 2)))) (else (* (/ 1 (* b (invariant-fast-expt b (- n 1)))) (* b (invariant-fast-expt b (- n 1))))))) (define (square x) (* x x)) (define (even? n) (= (remainder n 2) 0)) ( ;; Exercise 1.35 (define tolerance 0.00001) (define (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (if (close-enough? guess next) next (try next)))) (try first-guess)) (define (golden-ratio guess) (fixed-point (lambda (x) (+ 1 (/ 1 x))) guess)) ( ;; Exercise 1.37 ; a) (define (cont-frac n d k) (if (equal? k 0) 0 (/ n (+ d (cont-frac n d (- k 1)))))) ; K must be at least 11 in order to get an approximation ; that is accurate to 4 decimal places. ; ; b) (define (cont-frac-iter n d k x) (if (equal? k 0) x (cont-frac-iter n d (- k 1) (/ n (+ d x))))) ( ;; Exercise 1.38
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/30/2010 for the course EECS 21281 taught by Professor Harvey during the Spring '10 term at University of California, Berkeley.

Page1 / 2

hw3 - ; #1. ; Exercise 1.16 (define (invariant-fast-expt b...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online