Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 1/1.1.1.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#lang sicp

; Exercise 1.1.1
; Below is a sequence of expressions.
; What is the result printed by the interpreter in response to each expression?
; Assume that the sequence is to be evaluated in the order in which it is presented.

10
;10

(+ 5 3 4)
;12

(- 9 1)
;-8

(/ 6 2)
;3

(+ (* 2 4) (- 4 6))
;6
15 changes: 15 additions & 0 deletions 1/1.1.2.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#lang sicp

; Exercise 1.1.2
; Below is a sequence of expressions.
; What is the result printed by the interpreter
; in response to each expression? Assume that the sequence is to be
; evaluated in the order in which it is presented.

(define a 3)
(define b (+ a 1))
(+ a b (* a b))
(= a b)

;19
;#f
10 changes: 10 additions & 0 deletions 1/1.2.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#lang sicp

; Exercise 1.2
; Translate the following expression into prefix form.
; (5 + 4 + ( 2 − ( 3 − ( 6 + 4/5 ) ) ) ) / ( 3 ( 6 − 2 ) ( 2 −7 ) )

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

; Answer = -37/150
20 changes: 20 additions & 0 deletions 1/1.3.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#lang sicp

; Exercise 1.3
; Define a procedure that takes three numbers as arguments and
; returns the sum of the squares of the two larger numbers.


(define (square x)
(* x x))

(define (square-sum-larger a b c)
(cond ((and (> a b) (> b c))
(+ (square a) (square b)))
((and (> b a) (> c a))
(+ (square b) (square c)))
((and (> a b) (> c b))
(+ (square a) (square c)))))

(square-sum-larger 4 5 6)
; square sum of 5 and 6 is 61
19 changes: 19 additions & 0 deletions 1/1.4.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#lang sicp

; Exercise 1.4
; Observe that our model of evaluation allows for
; combinations whose operators are compound expressions.
; Use this observation to describe the behavior of the following procedure:

(define (mystery a b)
((if (> b 0) + -) a b))

; Describe the behavior of the procedure by selecting
; which descriptions are equivalent.
; If you think that the procedure definition is invalid, choose the reasons why.


;Answer
; The mystery procedure checks to see if b > 0. If true
; It adds (a + b)
; Else it subtracts a and b => a - -b => a + b
28 changes: 28 additions & 0 deletions 1/1.5.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#lang sicp

; Exercise 1.5
; Ben Bitdiddle has invented a test to determine whether the
; interpreter he is faced with is using applicative-order evaluation
; or normal-order evaluation. He defines the following two procedures:

(define (p) (p))

(define (test x y)
(if (= x 0)
0
y))

; Then he evaluates the expression

(test 0 (p))

; What behavior will Ben observe with an interpreter that uses applicative-order
; evaluation? What behavior will he observe with an interpreter that uses normal-order
; evaluation? Explain your answer. (Assume that the evaluation rule for the special form if
; is the same whether the interpreter is using normal or applicative order: The predicate
; expression is evaluated first, and the result determines whether to evaluate the consequent
; or the alternative expression.)

;Answer
; In applicative order, you recursively call (p) infintely
; In normalative order eval the if statement is true and the answer is 0
40 changes: 40 additions & 0 deletions 1/1.6.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#lang sicp

; Exercise 1.6
; Alyssa P. Hacker doesn’t see why if needs to be provided as
; a special form. “Why can’t I just define it as an ordinary
; procedure in terms of cond?” she asks. Alyssa’s friend Eva Lu Ator
; claims this can indeed be done, and she defines a new version of if:

(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))

; Eva demonstrates the program for Alyssa:

(new-if (= 2 3) 0 5)

5

(new-if (= 1 1) 0 5)

0

; Delighted, Alyssa uses new-if to rewrite the square-root program:

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

; What happens when Alyssa attempts to use this to compute square roots? Explain.

;Answer

; Scheme works in applicative order meaning the inner most definition is
; evaluated before a procedure happens. However `if` works by short circuiuting
; and if the first condition is true, the fucntion exits with `then` result and does not
; check `else`

; However in the above case, as we are using `cond`, the applicative order nature of scheme
; keeps recurisvely calling the `sqrt-iter` function infintely
50 changes: 50 additions & 0 deletions 1/1.7.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#lang sicp

; Exercise 1.7

; The good-enough? test used in computing square roots will not be very effective
; for finding the square roots of very small numbers. Also, in real computers, arithmetic
; operations are almost always performed with limited precision. This makes our test
; inadequate for very large numbers. Explain these statements, with examples showing
; how the test fails for small and large numbers.

; An alternative strategy for implementing good-enough? is to watch how guess changes
; from one iteration to the next and to stop when the change is a very small fraction of the
; guess. Design a square-root procedure that uses this kind of end test. Does this work
; better for small and large numbers?

(define (square x)
(* x x))

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

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

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

(define (good-enough? previous-guess guess)
(< (abs (/ (- guess previous-guess) guess)) 0.001))



(sqrt-iter 1.0 0.0005)
(sqrt-iter 1.0 1234567890123)
(sqrt-iter 1.0 12345678901234)

; Returns 0.036 as the answer when the real answer is 0.022
; Since the good enough function only check to see wether the
; sq root is within 0.001 of the answer, for very small numbers
; this method is not efficient for calculating the root

; if we try to find the square root of 1234567890123 using the
; good-enough? test with a tolerance of 0.001, the guess will
; eventually be rejected, even if it is very close to the actual square root.


; The alternate strat fixes these issues
29 changes: 29 additions & 0 deletions 1/1.8.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#lang sicp

; Exercise 1.8

; Newton's method for cube roots is based on the fact that if y is an approximation
; to the cube root of x, then a better approximation is given by the value

; ((x / y^2 ) + 2y ) / 3

; Use this formula to implement a cube-root procedure analogous to the square-root
; procedure. (In Section 1.3.4 we will see how to implement Newton’s method in general as an
; abstraction of these square root and cube-root procedures.)

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

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


(define (good-enough? previous-guess guess)
(< (abs (/ (- guess previous-guess) guess)) 0.0001))


; Reimplemented previous exercise with updated formula for improving
; cube root guesses