Browse Source

ch1 complete

canon
ix 4 years ago
parent
commit
f2643f17cc
8 changed files with 159 additions and 3 deletions
  1. +35
    -0
      ch1/ex40.scm
  2. +5
    -0
      ch1/ex41.scm
  3. +2
    -0
      ch1/ex42.scm
  4. +7
    -0
      ch1/ex43.scm
  5. +19
    -0
      ch1/ex44.scm
  6. +38
    -0
      ch1/ex45.scm
  7. +22
    -0
      ch1/ex46.scm
  8. +31
    -3
      ch1/procedures-as-returned-values.scm

+ 35
- 0
ch1/ex40.scm View File

@@ -0,0 +1,35 @@
(define (square x)
(* x x))

(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 dx 0.00001)

(define (deriv g)
(lambda (x) (/ (- (g (+ x dx)) (g x)) dx)))

(define (cube x)
(* x x x))

(define (newton-transform g)
(lambda (x) (- x (/ (g x) ((deriv g) x)))))

(define (newtons-method g guess)
(fixed-point (newton-transform g) guess))

(define (cubic a b c)
(lambda (x) (+ (cube x)
(* a (square x))
(* b x)
c)))

+ 5
- 0
ch1/ex41.scm View File

@@ -0,0 +1,5 @@
(define (double f)
(lambda (x) (f (f x))))

(define (inc x)
(+ x 1))

+ 2
- 0
ch1/ex42.scm View File

@@ -0,0 +1,2 @@
(define (compose f g)
(lambda (x) (f (g x))))

+ 7
- 0
ch1/ex43.scm View File

@@ -0,0 +1,7 @@
(define (compose f g)
(lambda (x) (f (g x))))

(define (repeated f n)
(if (< n 1)
(lambda (x) x)
(compose f (repeated f (- n 1)))))

+ 19
- 0
ch1/ex44.scm View File

@@ -0,0 +1,19 @@
(define (compose f g)
(lambda (x) (f (g x))))

(define (repeated f n)
(if (< n 1)
(lambda (x) x)
(compose f (repeated f (- n 1)))))

(define dx 0.00001)

(define (smooth f)
(lambda (x)
(/ (+ (f (- x dx))
(f x)
(f (+ x dx)))
3)))

(define (n-smooth f n)
((repeated smooth n) f))

+ 38
- 0
ch1/ex45.scm View File

@@ -0,0 +1,38 @@
(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 (average x y)
(/ (+ x y) 2))

(define (average-damp f)
(lambda (x) (average x (f x))))

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

(define (repeated f n)
(if (< n 1)
(lambda (x) x)
(compose f (repeated f (- n 1)))))

(define (get-max-pow n)
(define (iter p r)
(if (< (- n r) 0)
(- p 1)
(iter (+ p 1) (* r 2))))
(iter 1 2))

(define (nth-root n x)
(fixed-point ((repeated average-damp (get-max-pow n))
(lambda (y) (/ x (expt y (- n 1)))))
1.0))

+ 22
- 0
ch1/ex46.scm View File

@@ -0,0 +1,22 @@
;; returns a procedure that takes a guess and keeps improving
;; until good enough
(define (iterative-improve good-enough? improve)
(lambda (guess x) ; alternatively: (lambda (guess) (lambda (x) ...))
(define (iter guess)
(if (good-enough? guess x)
guess
(iter (improve guess x))))
(iter guess)))

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

(define (average x y)
(/ (+ x y) 2))

(define (nice-sqrt guess x)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
((iterative-improve good-enough? improve) guess x))

+ 31
- 3
ch1/procedures-as-returned-values.scm View File

@@ -1,3 +1,6 @@
(define (square x)
(* x x))

(define tolerance 0.00001)

(define (fixed-point f first-guess)
@@ -17,10 +20,35 @@
(define (average-damp f)
(lambda (x) (average x (f x))))

(define (nice-sqrt x)
(fixed-point (average-damp (lambda (y) (/ x y)))
1.0))
;(define (nice-sqrt x)
; (fixed-point (average-damp (lambda (y) (/ x y)))
; 1.0))

(define (cube-root x)
(fixed-point (average-damp (lambda (y) (/ x (square y))))
1.0))

(define dx 0.00001)

(define (deriv g)
(lambda (x) (/ (- (g (+ x dx)) (g x)) dx)))

(define (cube x)
(* x x x))

(define (newton-transform g)
(lambda (x) (- x (/ (g x) ((deriv g) x)))))

(define (newtons-method g guess)
(fixed-point (newton-transform g) guess))

;(define (nice-sqrt x)
; (newtons-method
; (lambda (y) (- (square y) x)) 1.0))

(define (fixed-point-of-transform g transform guess)
(fixed-point (transform g) guess))

(define (nice-sqrt x)
(fixed-point-of-transform
(lambda (y) (- (square y) x)) newton-transform 1.0))

Loading…
Cancel
Save