(define fixed-point (lambda (f first-guess) (define tolerance 0.00001) (define close-enough? (lambda (v1 v2) (< (abs (- v1 v2)) tolerance))) (define try (lambda (guess) (let ([next (f guess)]) (if (close-enough? guess next) next (try next))))) (try first-guess))) (define average (lambda (x y) (/ (+ x y) 2.0))) (define dis-fixed-point (lambda (f first-guess) (define tolerance 0.00001) (define close-enough? (lambda (v1 v2) (< (abs (- v1 v2)) tolerance))) (define try (lambda (guess) (let ([next (f guess)]) (display guess) (newline) (if (close-enough? guess next) next (try next))))) (try first-guess)))
> (dis-fixed-point (lambda (x) (+ 1 (/ 1.0 x))) 1.0)
1.0
2.0
1.5
1.6666666666666665
1.6
1.625
1.6153846153846154
1.619047619047619
1.6176470588235294
1.6181818181818182
1.6179775280898876
1.6180555555555556
1.6180257510729614
1.6180371352785146
1.6180327868852458
(dis-fixed-point (lambda (x) (/ (log 1000) (log x))) 2.0)
2.0
9.965784284662087
3.004472209841214
6.279195757507157
3.759850702401539
5.215843784925895
4.182207192401397
4.8277650983445906
4.387593384662677
4.671250085763899
4.481403616895052
4.6053657460929
4.5230849678718865
4.577114682047341
4.541382480151454
4.564903245230833
4.549372679303342
4.559606491913287
4.552853875788271
4.557305529748263
4.554369064436181
4.556305311532999
4.555028263573554
4.555870396702851
4.555315001192079
4.5556812635433275
4.555439715736846
4.555599009998291
4.555493957531389
4.555563237292884
4.555517548417651
4.555547679306398
4.555527808516254
4.555540912917957
4.555532270803653
浙公网安备 33010602011771号