derosa


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

About Me

No description provided.

Classes

How to Design Programs

Class status: Under Construction
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 1, HW 1

A little late, but had no time :/

  • Exercise 1.1 10 12 8 3 6 a (No output) b (No output) 19 false 4 16 6 16

  • Exercise 1.4 It checks the value of b, so if it's positive, the '+' operator is used. If it's negative, the '-' operator is used. So the procedure adds the absolute value of b to a.

  • Exercise 1.5 With normal-order evaluation, the interpreter will not try to evaluate (p) until it is needed, so it will check if 'x' equals zero and then print the 0 value. As (p) is never needed, it won't be evaluated. With applicative-order evaluation, the interpreter will try to evaluate (p). As (p) is defined as itself, the substitution will occur over and over again, leading to an infinite loop.

  • Exercise 1.6 The applicative-order evaluation will try to evaluate the sqrt-iter recursive call that comes in the "else-clause", leading to an infinite llop.

  • Exercise 1.7 With small numbers the good-enough? test will give a positive when the number is lower than the precision limit. With big numbers will occur in similar ways, being the number too big, the precision check may not be reached, so the computation will never stop. Code in the Code field.

* Exercise 1.2

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

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

* Exercise 1.3

(define (max x y z)
    (cond 
        ((and (> x y) (> y z))
            (+ (* x x) (* y y)))
        ((and (> x y) (> z y))
            (+ (* x x) (* z z)))
        ((and (> y x) (> z x))
            (+ (* y y) (* z z)))
    )
)


* Exercise 1.7

(define (sqrt-iter2 old guess x)
    (if (good-enough2? old guess x)
        guess
    (sqrt-iter2 guess (improve guess x) x)))

(define (good-enough2? old guess x)
    (< (abs (- 1 (/ guess old) ))
        0.001))

(define (improve guess x)
    (average guess (/ x guess)))

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

(define (raiz2 x)
    (sqrt-iter2 100 1.0 x))

* Exercise 1.8

(define (cuberoot x)
    (cuberoot-iter 100.0 1.0 x))

(define (cuberoot-iter old guess x)
    (if (good-enough old guess x)
        guess
        (cuberoot-iter guess (improve guess x) x)))

(define (good-enough old guess x)
    (< (abs (- old guess)) 0.001))

(define (improve guess x)
; ((x/y^2) + 2y) / 3
    (/ 
        (+ 
            (* 2 guess) 
            (/ 
                x 
                (square guess))) 
        3))


derosa 2 years ago