meowzero


Joined 2 years ago
Homeworks submitted:
Homework comments:
4
2

About Me

No description provided.

Classes

Structure and Interpretation of Computer Programs

Class status: Established
Role: Student
. 30% complete

Submitted Assignments

Structure and Interpretation of Computer Programs: Lesson 4, HW 1
;; Exercise 2.1
(define (make-rat n d)
  (let ((g (gcd n d)))
    (cond ((and (< n 0) (< d 0)) (cons (/ (abs n) g) (/ (abs d) g)))
          ((< d 0) (cons (/ (- 0 n) g) (/ (abs d) g)))
          (else (cons (/ n g) (/ d g))))))

;; Exercise 2.2

(define (print-point p)
  (newline)
  (display "(")
  (display (x-point p))
  (display ",")
  (display (y-point p))
  (display ")"))

(define (x-point p) (car p))
(define (y-point p) (cdr p))
(define (make-point x y) (cons x y))

(define (make-segment start end) (cons start end))
(define (start-segment points) (car points))
(define (end-segment points) (cdr points))

(define (midpoint-segment line-segment)
  (let ((start (start-segment line-segment))
        (end (end-segment line-segment)))
    (let ((start-x (x-point start))
          (start-y (y-point start))
          (end-x (x-point end))
          (end-y (y-point end)))
      (make-point (/ (+ start-x end-x) 2) (/ (+ start-y end-y) 2)))))

;; Exercise 2.4

(define (cdr z)
  (z (lambda (p q) q)))


;; Exercise 2.6

(define zero (lambda (f) (lambda (x) x)))

(define (add-1 n)
  (lambda (f) (lambda (x) (f ((n f) x)))))

(define one (lambda (f) (lambda (x) (f x))))

(define two (lambda (f) (lambda (x) (f (f x)))))

(define (+ a b)
  (lambda (f) (lambda (x) ((a f) (b f) x))))

;; Exercise 2.7
(define (make-interval a b) (cons a b))
(define (upper-bound interval)(car interval))
(define (lower-bound interval)(cdr interval))


;; Exercise 2.8
(define (sub-interval x y)
  (make-interval (- (lower-bound x) (upper-bound y))
                 (- (upper-bound x) (lower-bound y))))

meowzero 2 years ago
Structure and Interpretation of Computer Programs: Lesson 3, HW 1

Exercise 1.29

(integral cube 0 1 100)

0.24999999999999992

(integral cube 0 1 1000)

0.2500000000000002

Exercise 1.35

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

1.6180327868852458

Exercise 1.37

k needs to be 12 to be approx to 4 decimal places.

Exercise 1.41

value returned is 21

;; Exercise 1.29

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

(define (cube n)
  (* n n n))

(define (inc n)
  (+ n 1))

(define (integral f a b n)
  (define h
    (/ (- b a) n))
  (define (y f a k h)
    (f (+ a (* k h))))
  (define (term count)
    (* (check count) (y f a count h)))
  (define (check count)
    (cond ((or (= count 0) (= count n)) 1)
          ((even? count) 2.0)
          (else 4.0)))
  (* (/ h 3.0) (sum term 0 inc n)))


;; Exercise 1.30

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


;; Exercise 1.31
;; a)

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

(define (inc n)
  (+ n 1))

(define (prod-pi a b)
  (define (pi-term a)
    (define (numerator n)
      (if (odd? n) (+ n 1.0)
          (+ n 2.0)))
    (define (denominator n)
      (if (odd? n) (+ n 2.0)
          (+ n 1.0)))
    (/ (numerator a) (denominator a)))
  (product pi-term a inc b))

;; b)
(define (product2 term a next b)
  (define (iter a result)
    (if (> a b ) result
        (iter (next a) (* result (term a)))))
  (iter a (term a)))

;;Exercise 1.37
;;a)
(define (cont-frac n d k)
  (define (recur count)
    (if (= count k)
        (/ (n k) (d k))
        (/ (n k) (+ (d k) (recur (+ 1 count))))))
  (recur 1))

;;b)

(define (cont-frac n d k)
  (define (iter count result)
    (if (= count 1)
        result
        (iter (- count 1) (/ (n k) (+ (d k) result)))))
  (iter k (/ (n k) (d k))))


;; Exercise 1.39

(define (tan-cf x k)
  (cont-frac (lambda (i) 
               (if (= 1 i) x
                   (* x x)))
             (lambda (i) (- (* 2 i) 1))
             k))

;; Exercise 1.41

(define (double f)
  (lambda (n) (f (f n))))


;; Exercise 1.42

(define (compose f1 f2)
  (lambda (n)
    (f1 (f2 n))))

(define (square n)
  (* n n))

;; Exercise 1.43

(define (compose f1 f2)
  (lambda (n)
    (f1 (f2 n))))

(define (repeated f n)
  (define (iter result count)
    (if (= count 1) result
        (iter (compose f result) (- n 1))))
  (iter f n))

(define (square n)
  (* n n))

meowzero 2 years ago
Structure and Interpretation of Computer Programs: Lesson 2, HW 1

I started this HW early, so I did some even numbered problems before I knew about the odd numbered only problems.

Exercise 1.9

(+ 4 5)

(if (= 4 0) 5 (inc (+ (dec 4) 5))))

(inc (+ 3 5))

(inc (if (= 3 0) 5 (inc (+ (dec 3) 5)))))

(inc (inc (+ 2 5)))

(inc (inc (if (= 2 0) 5 (inc (+ (dec 2) 5)))))

(inc (inc (inc (+ 1 5))))

(inc (inc (inc (if (= 1 0) 5 (inc (+ (dec 1) 5))))))

(inc (inc (inc (inc (+ 0 5)))))

(inc (inc (inc (inc (if (= 0 0) 5 (inc (+ (dec 0) 5)))))))

(inc (inc (inc (inc 5))))

(inc (inc (inc 6)))

(inc (inc 7))

(inc 8)

9 Recursive

(+ 4 5)

(if (= 4 0) 5 (+ (dec 4) (inc 5)))

(+ (dec 4) (inc 5)))

(+ 3 6)

(if (= 3 0) 6 (+ (dec 3) (inc 6)))

(+ (dec 3) (inc 6))

(+ 2 7)

(if (= 2 0) 7 (+ (dec 2) (inc 7)))

(+ (dec 2) (inc 7))

(+ 1 8)

(if (= 1 0) 8 (+ (dec 1 ) (inc 8)))

(+ (dec 1) (inc 8))

(+ 0 9)

(if (= 0 0) 9 (+ (dec 0) (inc 9)))

9 Iterative

Exercise 1.10

(A 1 10) = 1024

(A 2 4) = 65536

(A 3 3 ) = 65536

(f n) = 2n

(g n) = 2^n

(h n) = 2^(h (n-1))

Exercise 1.15

a) 5

b) O(log(a)) for both space and time

Exercise 1.19

??

Exercise 1.20

run forever for normal order? 4 times for applicative order

Exercise 1.21

199 -> 199

1999 -> 1999

19999 -> 7

1.23

??

Exercise 1.25

Yes, she is correct. Both methods should give the same answers. She is correct. But I don't know if this would serve well for our fast prime tester.

; Exercise 1.11
(define (f n)
    (if (< n 3) n
        (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))


(define (g n)
    (g-iter 2 1 0 n))

(define (g-iter a b c count)
  (if (< count 3) a
      (g-iter (+ a (* 2 b) (* 3 c)) a b (- count 1))))

; Exercise 1.12

(define (pascals-triangle row col)
  (cond ((or (= col 0) (> col row)) 0)
        ((= col row ) 1)
        ((= row 1) 1)
        (else (+ (pascals-triangle (- row 1) (- col 1))
                 (pascals-triangle (- row 1) col)))))
 

; Exercise 1.16
(define (fast-iter-exp b n)
  (iter b n 1))

(define (iter b n a)
  (cond ((= n 0) a)
        ((even? n) (iter (square b) (/ n 2) a))
        (else (* b (iter b (- n 1) a)))))

; Exercise 1.17
(define (double n)
  (+ n n))

(define (halve n)
  (/ n 2))

(define (* a b)
  (cond ((= b 0) 0)
        ((even? b) (* (double a) (halve b)))
        (else (+ a (* (double a) (halve (- b 1)))))))

; Exercise 1.27
(define (square x) (* x x)) 
  
(define (expmod base exp m) 
  (cond ((= exp 0) 1) 
        ((even? exp) 
         (remainder (square (expmod base (/ exp 2) m)) m)) 
        (else 
          (remainder (* base (expmod base (- exp 1) m)) m))))         
  
(define (fermat-test n)
  (define (fermat-iter n a)
    (cond ((= n a) #t)
          ((= (expmod a n n) a ) (fermat-iter n (+ a 1)))
          (else #f)))
  (fermat-iter n 1))

;The Carmichael numbers do not fool it. So something is wrong.

meowzero 2 years ago
Structure and Interpretation of Computer Programs: Lesson 1, HW 1

Exercise 1.1 10 12 8 3 6

19 4 16 6 16

Exercise 1.2 (/ (+ 5 4 (- 2 (- 3 (+ 6 ( / 4 3))))) (* 3 (- 6 2) (- 2 7)))

Exercise 1.3 (define (f x y z) (cond ((and (> x y) (> y z)) (+ ( x x) ( y y))) ((and (> x z) (> y x)) (+ ( x x) ( y y))) ((and (> y x) (> z x)) (+ ( y y) ( z z))) ((and (> z y) (> y x)) (+ ( y y) ( z z))) (else (+ ( x x ) ( z z)))))

Exercise 1.4 If b is negative we subtract a and b. Else, we add a and b.

Exercise 1.5

Exercise 1.6

(define (f x y z)
  (cond ((and (> x y) (> y z)) (+ (* x x) (* y y)))
        ((and (> x z) (> y x)) (+ (* x x) (* y y)))
        ((and (> y x) (> z x)) (+ (* y y) (* z z)))
        ((and (> z y) (> y x)) (+ (* y y) (* z z)))
         (else (+ (* x x ) (* z z)))))

meowzero 2 years ago