Skip to content

Instantly share code, notes, and snippets.

@kEND
kEND / gist:189612
Created September 19, 2009 22:28 — forked from redsquirrel/gist:189190
Exercise 1.1
SICP 1.1
10
=> 10
(+ 5 3 4)
=> 12
(- 9 1)
=> 8
(/ 6 2)
=> 3
(+ (* 2 4) (- 4 6))
@kEND
kEND / Exercise 1.2
Created September 19, 2009 22:42 — forked from redsquirrel/gist:189191
Exercise 1.2
SICP 1.2
(/ (+ 5 4
(- 2
(- 3
(+ 6
(/ 5 4)
)
)
)
)
@kEND
kEND / sicp-1-3.lisp
Created September 19, 2009 23:03 — forked from redsquirrel/gist:189192
Exercise 1.3
(define (square x) (* x x))
(define (sum-of-squares x y) (+ (square x) (square y)))
(define (larger x y) (if (> x y) x y))
(define (sum-squares-of-largest-two x y z)
(cond ((and (> x y) (> x z)) (sum-of-squares x (larger y z)))
((and (> y x) (> y z)) (sum-of-squares y (larger x z)))
(else (sum-of-squares z (larger x y)))
@kEND
kEND / sicp-1-4.lisp
Created September 20, 2009 01:22 — forked from redsquirrel/gist:189193
Exercise 1.4
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b)
b is evaluated to be greater than zero or not
based on that evaluation the + or - operator is selected
and used to add a and b if b is positive or subtract b from a if b is negative
@kEND
kEND / sicp-1-5.lisp
Created September 20, 2009 02:04 — forked from jimweirich/gist:189248
Exercise 1.5
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
applicative-order evaluation
evaluate arguments and then apply
normal-order evaluation
;; new-if....
With the new-if, things look good when the case is simple and it doesn't matter if the then-clause and else-clause get evaluated. Using the special form if, first the predicate will be evaluated and then the then-clause OR the else-clause. With the new-if procedure, all arguments will be evaluated first. This sqrt-iter will not progress to any result. Infinite loop.
@kEND
kEND / sicp-1-7.lisp
Created September 20, 2009 03:31 — forked from jimweirich/gist:189261
Exercise 1.7
;; good-enough?
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?