Commit d0e97325 authored by bbguimaraes's avatar bbguimaraes
Browse files

sicp: section 1.3

parent 07b83d9a
(define (sum-integers a b)
(if (> a b)
0
(+ a (sum-integers (+ a 1) b))))
(display (sum-integers 1 10))
(define (cube x) (* x x x))
(define (sum-cubes a b)
(if (> a b)
0
(+ (cube a) (sum-cubes (+ a 1) b))))
(newline)
(display (sum-cubes 1 10))
(define (pi-sum a b)
(if (> a b)
0
(+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))
(newline)
(display (* 8 (pi-sum 1 1000)))
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (inc n) (+ n 1))
(define (sum-cubes a b)
(sum cube a inc b))
(newline)
(newline)
(display (sum-cubes 1 10))
(define (identity x) x)
(define (sum-integers a b)
(sum identity a inc b))
(newline)
(display (sum-integers 1 10))
(define (pi-sum a b)
(define (pi-term x)
(/ 1.0 (* x (+ x 2))))
(define (pi-next x)
(+ x 4))
(sum pi-term a pi-next b))
(newline)
(display (* 8 (pi-sum 1 1000)))
(define (integral f a b dx)
(define (add-dx x) (+ x dx))
(* (sum f (+ a (/ dx 2.0)) add-dx b)
dx))
(newline)
(newline)
(display (integral cube 0 1 0.01))
(newline)
(display (integral cube 0 1 0.001))
(newline)
(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ result (term a)))))
(iter a 0))
(define (pi-sum a b)
(sum (lambda (x) (/ 1.0 (* x (+ x 2))))
a
(lambda (x) (+ x 4))
b))
(display (* 8 (pi-sum 1 1000)))
(newline)
(define (integral f a b dx)
(* (sum f
(+ a (/ dx 2.0))
(lambda (x) (+ x dx))
b)
dx))
(display (integral (lambda (x) (* x x x)) 0 1 0.001))
(newline)
(display ((lambda (x y z) (+ x y (square z))) 1 2 3))
(newline)
(define (f x y)
(define (f-helper a b)
(+ (* x (square a))
(* y b)
(* a b)))
(f-helper (+ 1 (* x y))
(- 1 y)))
(newline)
(display (f 2 3))
(newline)
(define (f x y)
((lambda (a b)
(+ (* x (square a))
(* y b)
(* a b)))
(+ 1 (* x y))
(- 1 y)))
(display (f 2 3))
(newline)
(define (f x y)
(let ((a (+ 1 (* x y)))
(b (- 1 y)))
(+ (* x (square a))
(* y b)
(* a b))))
(display (f 2 3))
(newline)
(define x 5)
(display (+ (let ((x 3))
(+ x (* x 10)))
x))
(newline)
(define x 2)
(display (let ((x 3)
(y (+ x 2)))
(* x y)))
(newline)
(define (f x y)
(define a (+ 1 (* x y)))
(define b (- 1 y))
(+ (* x (square a))
(* y b)
(* a b)))
(display (f 2 3))
(newline)
(define (average x y)
(/ (+ x y) 2))
(define (close-enough? x y)
(< (abs (- x y)) 0.001))
(define (search f neg-point pos-point)
(let ((midpoint (average neg-point pos-point)))
(if (close-enough? neg-point pos-point)
midpoint
(let ((test-value (f midpoint)))
(cond ((positive? test-value)
(search f neg-point midpoint))
((negative? test-value)
(search f midpoint pos-point))
(else midpoint))))))
(define (half-interval-method f a b)
(let ((a-value (f a))
(b-value (f b)))
(cond ((and (negative? a-value) (positive? b-value))
(search f a b))
((and (negative? b-value) (positive? a-value))
(search f b a))
(else (error "Values are not of opposite sign" a b)))))
(display (half-interval-method sin 2.0 4.0))
(newline)
(display (half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3 ))
1.0
2.0))
(newline)
(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))
(display (fixed-point cos 1.0))
(newline)
(display (fixed-point (lambda (y) (+ (sin y) (cos y)))
1.0))
(newline)
(define (sqrt x)
(fixed-point (lambda (y) (average y (/ x y)))
1.0))
(display (sqrt 4))
(newline)
(define (average x y) (/ (+ x y) 2))
(define (average-damp f)
(lambda (x) (average x (f x))))
(display ((average-damp square) 10))
(newline)
(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 (sqrt x)
(fixed-point (average-damp (lambda (y) (/ x y)))
1.0))
(display (sqrt 9))
(newline)
(define (cube-root x)
(fixed-point (average-damp (lambda (y) (/ x (square y))))
1.0))
(display (cube-root 27))
(newline)
(define (deriv g)
(lambda (x)
(/ (- (g (+ x dx)) (g x))
dx)))
(define dx 0.00001)
(define (cube x) (* x x x))
(display ((deriv cube) 5))
(newline)
(define (newton-transform g)
(lambda (x)
(- x (/ (g x) ((deriv g) x)))))
(define (newtons-method g guess)
(fixed-point (newton-transform g) guess))
(define (sqrt x)
(newtons-method (lambda (y) (- (square y) x))
1.0))
(display (sqrt 9))
(newline)
(define (fixed-point-of-transform g transform guess)
(fixed-point (transform g) guess))
(define (sqrt x)
(fixed-point-of-transform (lambda (y) (/ x y))
average-damp
1.0))
(display (sqrt 9))
(newline)
(define (sqrt x)
(fixed-point-of-transform (lambda (y) (- (square y) x))
newton-transform
1.0))
(display (sqrt 9))
(newline)
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
(define (cube x) (* x x x))
(define (inc x) (+ x 1))
(define (simpsons-rule f a b n)
(define (with-h h)
(define (y k) (f (+ a (* k h))))
(define (term k)
(* (cond ((or (= k 0) (= k n)) 1)
((odd? k) 4)
(else 2))
(y k)))
(* (/ h 3.0) (sum term 0 inc n)))
(with-h (/ (- b a) n)))
(display (simpsons-rule cube 0 1 100.0))
(newline)
(display (simpsons-rule cube 0 1 1000.0))
(newline)
(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ result (term a)))))
(iter a 0))
(define (inc n) (+ n 1))
(define (identity x) x)
(define (sum-integers a b)
(sum identity a inc b))
(display (sum-integers 1 10))
(define (cube x) (* x x x))
(define (sum-cubes a b)
(sum cube a inc b))
(newline)
(display (sum-cubes 1 10))
(define (pi-sum a b)
(define (pi-term x)
(/ 1.0 (* x (+ x 2))))
(define (pi-next x)
(+ x 4))
(sum pi-term a pi-next b))
(newline)
(display (* 8 (pi-sum 1 1000)))
(newline)
(define (integral f a b dx)
(define (add-dx x) (+ x dx))
(* (sum f (+ a (/ dx 2.0)) add-dx b)
dx))
(newline)
(display (integral cube 0 1 0.01))
(newline)
(display (integral cube 0 1 0.001))
(newline)
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* result (term a)))))
(iter a 1))
(define (identity x) x)
(define (inc x) (+ x 1))
(define (factorial n)
(product identity 1 inc n))
(display (factorial 6))
(newline)
(define (pi-product n)
(define (term x)
(define (result n d)
(/ n d))
(if (odd? x) (result (+ x 1) (+ x 2))
(result (+ x 2) (+ x 1))))
(* (product term 1.0 inc n)
4.0))
(display (pi-product 100.0))
(newline)
(display (pi-product 1000.0))
(newline)
(define (product term a next b)
(define (iter a)
(if (> a b)
1
(* (term a) (iter (next a)))))
(iter a))
(display (factorial 6))
(newline)
(display (pi-product 100.0))
(newline)
(display (pi-product 1000.0))
(newline)
(define (accumulate combiner null-value term a next b)
(if (> a b)
null-value
(combiner (term a) (accumulate combiner null-value term (next a) next b))))
(define (sum term a next b)
(accumulate + 0 term a next b))
(define (product term a next b)
(accumulate * 1 term a next b))
(define (inc n) (+ n 1))
(define (identity x) x)
(define (cube x) (* x x x))
(define (sum-integers a b)
(sum identity a inc b))
(define (sum-cubes a b)
(sum cube a inc b))
(define (pi-sum a b)
(define (pi-term x)
(/ 1.0 (* x (+ x 2))))
(define (pi-next x)
(+ x 4))
(sum pi-term a pi-next b))
(define (integral f a b dx)
(define (add-dx x) (+ x dx))
(* (sum f (+ a (/ dx 2.0)) add-dx b)
dx))
(define (factorial n)
(product identity 1 inc n))
(define (pi-product n)
(define (term x)
(define (result n d)
(/ n d))
(if (odd? x) (result (+ x 1) (+ x 2))
(result (+ x 2) (+ x 1))))
(* (product term 1.0 inc n)
4.0))
(display (sum-integers 1 10))
(newline)
(display (sum-cubes 1 10))
(newline)
(display (* 8 (pi-sum 1 1000)))
(newline)
(display (integral cube 0 1 0.01))
(newline)
(display (integral cube 0 1 0.001))
(newline)
(display (factorial 6))
(newline)
(display (pi-product 100.0))
(newline)
(display (pi-product 1000.0))
(newline)
(define (accumulate combiner null-value term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (combiner (term a) result))))
(iter a null-value))
(newline)
(display (sum-integers 1 10))
(newline)
(display (sum-cubes 1 10))
(newline)
(display (* 8 (pi-sum 1 1000)))
(newline)
(display (integral cube 0 1 0.01))
(newline)
(display (integral cube 0 1 0.001))
(newline)
(display (factorial 6))
(newline)
(display (pi-product 100.0))
(newline)
(display (pi-product 1000.0))
(newline)
(define (filtered-accumulate filter combiner null-value term a next b)
(if (> a b)
null-value
(if (filter a)
(combiner (term a)
(filtered-accumulate
filter combiner null-value term (next a) next b))
(filtered-accumulate filter combiner null-value term (next a) next b))))
(define (identity x) x)
(define (inc x) (+ x 1))
(define (prime? n)
(define (smallest-divisor n)
(define (find-divisor n test-divisor)
(define (divides? a b)
(= (remainder b a) 0))
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(find-divisor n 2))
(= n (smallest-divisor n)))
(define (sum-squares-primes a b)
(filtered-accumulate prime? + 0 square a inc b))
(display (sum-squares-primes 2 10))
(newline)
(define (product-relative-primes n)
(define (relative-prime? x) (= (gcd n x) 1))
(filtered-accumulate relative-prime? * 1 identity 1 inc n))
(display (product-relative-primes 10))
(newline)
(define (filtered-accumulate filter combiner null-value term a next b)
(define (iter a result)
(cond ((> a b) result)
((filter a) (iter (next a) (combiner (term a) result)))
(else (iter (next a) result))))
(iter a null-value))
(newline)
(display (sum-squares-primes 2 10))
(newline)
(display (product-relative-primes 10))
(newline)
(define (f g)
(g 2))
(display (f square))
(display " ")
(display (f (lambda (z) (* z (+ z 1)))))
(display " ")
(display (f f))
(newline)
(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))
(display (fixed-point (lambda (x) (+ 1 (/ 1 x)))
1.0))
(newline)
(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 (printing-fixed-point f first-guess)
(define (close-enough? v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
(display next)
(newline)
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(define (average x y) (/ (+ x y) 2))
(display (printing-fixed-point (lambda (x) (/ (log 1000) (log x)))
2.0))
(newline)
(newline)
(display (printing-fixed-point (lambda (x) (average x (/ (log 1000) (log x))))
2.0))
(newline)
(define (phi k)
(cont-frac (lambda (i) 1.0)
(lambda (i) 1.0)
k))
(define (cont-frac n d k)
(define (rec i)
(if (> i k)
0
(/ (n i) (+ (d i) (rec (+ i 1))))))
(rec 1))
(display (/ 1 (phi 100)))