码迷,mamicode.com
首页 > 其他好文 > 详细

SICP练习1.6-1.8

时间:2015-11-28 10:33:41      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

1.6  

死循环

1.7

 1 #lang racket
 2 (define (square x) (* x x))
 3 (define (sqrt-iter guess x)
 4   (if (good-enough? guess x)
 5       guess
 6       (sqrt-iter (improve guess x)
 7                  x)))
 8 (define (improve guess x)
 9   (average guess (/ x guess)))
10 
11 (define (average x y)
12   (/ (+ x y) 2))
13 
14 (define (good-enough? guess x)
15   (< (abs (- (/ (square guess) x) 1) ) 0.00001))
16 
17 (define (sqrt x)
18   (sqrt-iter 1.0 x))
19 
20 
21 (sqrt 0.002)

1.8立方根的牛顿法

 1 #lang racket
 2 (define (power3 x) (* x x x))
 3 (define (square x) (* x x))
 4 (define (curo-iter guess x)
 5   (if (good-enough? guess x)
 6       guess
 7       (curo-iter (improve guess x)
 8                  x)))
 9 (define (improve guess x)
10   (average3 (* 2 guess) (/ x (square guess))))
11 (define (average3 x y)
12   (/ (+ x y) 3))
13 (define (good-enough? guess x)
14   (< (abs (- (/ (power3 guess) x) 1) ) 0.00001))
15 (define (curo x)
16   (curo-iter 1.0 x))
17 
18 (curo 27)

 

SICP练习1.6-1.8

标签:

原文地址:http://www.cnblogs.com/Liqiongyu/p/5002232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!