TajMaholla


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

About Me

No description provided.

Classes

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

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 1, HW 1

This is my first shot at lisp (or a programming course of any kind) but I enjoyed it. Also, sorry if the formatting is crap.

Exercise 1.1

10 12 8 3 6 "a --> 3" "b --> 4" 19 f 4 16 6 16

Exercise 1.2

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

Exercise 1.3

(define (sumsq x y z) (if (> x y) (if (> y z) (+ ( x x) ( y y)) (+ ( x x) ( z z))) (if (or (> z y) (> z x)) (+ ( z z) ( y y)) (+ ( x x) ( y y))) ) )

Exercise 1.4

The procedure uses a conditional, in this case the if statement, in the place of an operator. This allows the function to evaluate the statement b>0 before determining which operator should be used in combining a and b.

Exercise 1.5

Applicative order evaluates all operators and operands before applying the resulting procedures. This causes the infinite loop procedure (p) to be evaluated before the if statement. If the interpreter used normal evaluation the if statement would be evaluated before the (p) procedure was called and would cause the program to bypass (p) and return 0.

Exercise 1.6

Applicative order evaluation will cause both the then-clause and the else-clause to be evaluated which will lead to infinite recursion as the else clause has no exit point.

Exercise 1.7

For any number smaller than the cut off given in good-enough? the sqrt function will be inaccurate as it will stop once the square of the guess is less than the cut off. For large numbers, the number of iterations necessary to find an acceptable answer will lead to the accumulation of small arithmetic errors that are added in each step.

##Exercise 1.7 - Code

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


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


(define (good-enough? guess guess2)
  (< (abs (- guess guess2)) (* 0.00001)))


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

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

##Exercise 1.8

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

(define (good-enough? guess guess2)
  (< (abs (- guess guess2)) (* 0.00001 guess)))


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

(define (sqrt x)
  (sqrt-iter 1.0 x 3.0))


TajMaholla 2 years ago