Add 1.2, 1.3 and 1.7 exs

This commit is contained in:
NaiJi 2020-07-19 20:40:30 +03:00
commit d90eecd9bb
3 changed files with 41 additions and 0 deletions

7
sicp-ex-1.2.rkt Normal file
View File

@ -0,0 +1,7 @@
(define the-numerator
(+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))))
(define the-denominator
(* 3 (- 6 2)(- 2 7)))
(/ the-numerator the-denominator)

11
sicp-ex-1.3.rkt Normal file
View File

@ -0,0 +1,11 @@
(define (square x) (* x x))
(define (sum-of-squares x y) (+ (square x) (square y)))
(define (is-first-least? a b c) (and (< a b) (< a c)))
(define a 9)
(define b 1)
(define c 10)
(cond ((is-first-least? c a b)(sum-of-squares a b))
((is-first-least? a c b)(sum-of-squares c b))
((is-first-least? b a c)(sum-of-squares a c)))

23
sicp-ex-1.7.rkt Normal file
View File

@ -0,0 +1,23 @@
(define (sqrt-iter guess guess-prev x)
(if (good-enough? guess guess-prev)
guess
(sqrt-iter (improve guess x) guess
x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess guess-prev)
(< (abs (- guess guess-prev)) 0.001))
(define (sqrt-new x)
(if (= x 0) x (sqrt-iter 1.0 999 x)))
(sqrt-new 16)
(sqrt-new 1.0004)
(sqrt-new 1)
(sqrt-new 134895724398)
(sqrt-new 0)