# 红色壁虎（三磊）

Red Gecko 石子虽平凡，聚少亦成多。
::  ::  ::  ::  ::

1.1.7 牛顿法求平方根

(define (average x y)

(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (good-enough? guess x)
(< (abs (- (* guess guess) x) )0.0000000000001) )
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)
))
(define (sqrt-g x)
(sqrt-iter 1.0 x))

Welcome to DrRacket, version 5.1.1 [3m].
Language: R5RS; memory limit: 128 MB.
> (sqrt-g 2)
1.414213562373095
> (sqrt 2)
1.4142135623730951
> (sqrt-g 3)
1.7320508075688772
> (sqrt 3)
1.7320508075688772

>

;练习 1.6 .newif
(define (newif predicate  thenClause elseClause)
(cond (predicate thenClause)
(else elseClause)))
(define (newSqrt-iter guess x)
(newif (good-enough? guess x)
guess
(newsqrt-iter (improve guess x)
x)))
(define (newSqrt-g x)

(newSqrt-iter 1.0 x))

Welcome to DrRacket, version 5.1.1 [3m].
Language: R5RS; memory limit: 128 MB.
> (newSqrt-g 2)
Interactions disabled //因为超出内存使用量而终止，死循环，原因：函数使用 应用序 ，先求参数值后应用规则。

;练习1.7

(define (good-enough? guess x)
(< (abs (- (improve guess x) guess) )0.00000000000000001) )

> (sqrt 2000000000000000000000000000000000000000000000000)

1.414213562373095e+024

> (sqrt 0.0000000000000000000000000000000000000000000000002)

4.472135954999579e-025

>

;练习1.8
(define (CubeImprove y x)
(/ (+ ( / x (* y y)) (* 2 y)) 3))
(define (CubeGood? guess x)
(< (abs (- (CubeImprove guess x) guess)) 0.0000000001))
(define (Cube-iter guess x)
(if (CubeGood? guess x)
guess
(Cube-iter (CubeImprove guess x) x)))
(define (CubeGood? guess x)
(< (abs (- (CubeImprove guess x) guess)) 0.0000000001))
(define (Cube x)

(Cube-iter 1.0 x))

Welcome to DrRacket, version 5.1.1 [3m].

Language: R5RS; memory limit: 128 MB.
> (Cube 8)
2.000000000012062
> (Cube 27)
3.0000000000000977
> (Cube -64)
-4.000000000004644
> (Cube -125)
-5.000000000076353