scheme牛顿迭代 发表于 2016-03-16 | 分类于 scheme 求平方根 1234567891011121314151617181920212223242526272829303132(define (square x) (* x x))(square 2)(define (abs x) (if (< x 0) (- x) x) )(abs -1);;(define (goodenough guess x);; (< (abs (- (square guess) x)) 0.0001))(define (goodenough guess x) (< (abs (- (/ guess (average guess (/ x guess))) 1)) 0.01))(goodenough 3 9)(define (average a b) (/ (+ a b) 2))(average 9 3)(define (sqrt-iter guess x) (if (goodenough guess x) guess (sqrt-iter (average guess (/ x guess)) x)) )(sqrt-iter 1.0 9)(define (sqrt x) (sqrt-iter 1.0 x))(sqrt 9) 求立方根 123456789101112131415161718192021222324252627282930313233343536373839(define (square x) (* x x))(square 2)(define (cube x) (* x x x))(cubert 2)(define (abs x) (if (< x 0) (- x) x) )(abs -1)(abs 1)(define (goodenough guess x) (< (abs (- (/ guess (improve guess x)) 1)) 0.01))(goodenough 3 9)(define (improve guess x) (/ (+ (/ x (square guess)) (* 2 guess)) 3))(improve 1.0 9)(define (average a b) (/ (+ a b) 2))(average 9 3)(define (cubert-iter guess x) (if (goodenough guess x) guess (cubert-iter (improve guess x) x)) )(cubert-iter 2.0 27)(define (cubert x) (cubert-iter 1.0 x))(cubert 27) 推荐一首好听个歌给大家,希望大家都能成为自己的Best Of Me