grig


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

About Me

No description provided.

Classes

Structure and Interpretation of Computer Programs

Class status: Established
Role: Student
. 15% complete

Submitted Assignments

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

Didn't have time during the week and left it to last day. So I solved only these 3 problems. Will add others later.

  • 1.9

inc(+ 4 5)

inc(inc(+ 3 5))

inc(inc(inc(+ 2 5)))

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

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

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

inc(inc(inc(6)))

inc(inc(7))

inc(8)

9

(+ 4 5)

(+ 3 6)

(+ 2 7)

(+ 1 8)

(+ 0 9)

9

  • 1.15

a. 12.5/3/n ~ 0.1, n = 5

b. o(log(n))

1.11

recursive:

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

iterative:

(define (f-iter count sum1 sum2 sum3)
(cond ((= count 0) sum1)
(else (f-iter (- count 1) (+ sum1 (* 2 sum2) (* 3 sum3)) sum1 sum2)))
)

(define (f n)
(cond ((= n 0) 0)
((= n 1) 1)
((= n 2) 2)
(else (f-iter (- n 2) 2 1 0)))
)

grig 2 years ago
Structure and Interpretation of Computer Programs: Lesson 1, HW 1
  • 1.1 10 12 8 3 6 19 #f 4 16 6 16

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

  • 1.4 a+|b|

  • 1.5 applicative-order will hang, normal-order will return 0

  • 1.6 may fall into an infinite loop, but with my implementation od scheme (plt-scheme) it works as before

  • 1.7 for example doesn't work for these numbers: 0.0002 - wrong result 28736218733491 - hangs. improvement would be:

(define (good-enough? guess x) (< (abs (- (improve guess x) guess)) 0.001))

* 1.3
(define (max2sqr a b c) (cond
((or (and (>= a b) (>= b c)) (and (>= b a) (>= a c))) (+(* a a) (* b b )))
((or (and (>= a c) (>= c b)) (and (>= c a) (>= a b))) (+(* a a) (* c c )))
((or (and (>= b c) (>= c a)) (and (>= c b) (>= b a))) (+(* b b) (* c c )))
))

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

(define (good-enough? guess x)
  (< (abs (- (cube guess) x)) 0.001))

(define (sqrt-cube-iter guess x)
  (if (good-enough? guess x)
      guess
      (sqrt-cube-iter (improve-cube guess x)
                 x)))

(define (improve-cube y x)
  (/ (+(/ x (* y y))(* 2 y)) 3))

(define (my-sqrt-cube x)
  (sqrt-cube-iter 1.0 x))

grig 2 years ago