sgalkin


Joined 5 months ago

About Me

No description provided.

Classes

Bulletproof Web Design

Class status: Established
Role: Student
. 0% complete

Structure and Interpretation of Computer Programs

Class status: Established
Role: Student
. 7% complete

Submitted Assignments

Structure and Interpretation of Computer Programs: Lesson 3, HW 1
;; ====================================
;; exercise 1.29

(define (simpson-int f a b n)
  (define h (/ (- b a) n))
  (define (term k)
    (* (f (+ a (* k h)))
       (cond
         ((or (= k 0) (= k n)) 1)
         ((even? k) 2.0)
         (else 4.0))))

  (* (/ h 3.0)
     (sum term 0 inc n)))


;; (simpson-int cube 0 1 100)
;; > .24999999999999992
;;
;; This has the same order that
;; (integral cube 0 1 0.01)
;; > .24998750000000042
;; Which has larger error
;;
;;
;; (simpson-int cube 0 1 1000)
;; > .2500000000000002
;;
;; (integral cube 0 1 0.001)
;; > .249999875000001




;; ====================================
;; exercise 1.30

(define (sum-iter term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (+ result (term a)))))
  (iter a 0))


;; ====================================
;; exercise 1.31 a
;;

(define (product term a next b)
  (if (> a b)
      1
      (* (term a)
         (product term (next a) next b))))

(define (factorial n)
  (product identity 1 inc n))

(define (pi n)
  (define (inc2 x)
    (inc (inc x)))
  (define (term n)
    (/ (* n (inc2 n)) (* (inc n) (inc n))))
  (* 4.0 (product term 2 inc2 n)))


;; ====================================
;; exercise 1.31 b
;;

(define (product-iter term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (* result (term a)))))
  (iter a 1))


;; ====================================
;; exercise 1.35
;;
;; x = 1 + 1/x
;; x^2 - x - 1 = 0
;; (1 +- sqrt(5)) / 2

(fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0)


;; ====================================
;; exercise 1.37 a

(define (cont-frac n d k)
  (define (recur i)
    (if (= i k)
      (/ (n k) (d k))
      (/ (n i) (+ (d i) (recur (inc i))))))
  (recur 1))

(define (golden-inverse k)
  (cont-frac (lambda (i) 1.0)
             (lambda (i) 1.0)
             k))

;; (golden-inverse 1) => 1
;; (golden-inverse 2) => 0.5
;; (golden-inverse 3) => 0.6666666666666666
;; (golden-inverse 8) => 0.6176470588235294
;; (golden-inverse 100) => 0.6180339887498948

;; approx-order will calculate the needed order to get
;; 4 decimals precission

(define approx-order
  (let ((real-golden-inverse (/ 2 (+ 1 (sqrt 5)))))
    (define (recur k)
      (let ((error (abs (- (golden-inverse k) real-golden-inverse))))
        (if (< error 0.0001)
          k
          (recur (inc k)))))
  (recur 1)))

;; approx-order = 10


;; ====================================
;; exercise 1.37 b

(define (cont-frac-iter n d k)
  (define (dec i) (- i 1))
  (define (iter i summand)
    (if (= i 1)
      (/ (n 1) (+ (d 1) summand))
      (iter (dec i) (/ (n i) (+ (d i) summand)))))
  (iter k 0))


;; ====================================
;; exercise 1.39

(define (tan-cf x k)
  (- (cont-frac
       (lambda (_) (- (* x x)))
       (lambda (i) (- (* 2.0 i) 1))
       k)))


;; ====================================
;; exercise 1.41

(define (double f)
  (lambda (x) (f (f x))))

;; (((double (double double)) inc) 5) => 21


;; ====================================
;; exercise 1.42

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


;; ====================================
;; exercise 1.43

(define (repeated f n)
  (if (= n 1)
    f
    (repeated (compose f f) (- n 1))))

sgalkin 5 months ago