計算機プログラムの構造と解釈 第二版 P43 問題1.40

問題を読んでも特に難しいことはなさそうなイメージ。

念のため「零点」について調べておく。

零点
http://ja.wikipedia.org/wiki/%E9%9B%B6%E7%82%B9

零点(れいてん、ぜろてん、zero)とは、ある関数 f によって、0 に移される点、すなわち f(z) = 0 を満たす z のこと。


なるほど。
この問題は前のページの解説とかを読んでると結構簡単に解けて、
ある手続きを返す、手続きcubicを定義すればいいってことだ。


んで、問題から察するに、
(cubic a b c)
というふうにつかうと、
数学で言うところの
f(x) = x^3 + ax^2 + bx + c
が返るようにすればいい。


どういうことか、もうちょっと具体的にすると、
a: 2
b: 3
c: 4
とすると
(cubic 2 3 4)
とかやると、
f(x) = x^3 + 2x^2 + 3x + 4
みたいな感じの手続きが返るようにすればよいのだ。


ということで、、、ソース

#!/usr/local/bin/gosh
;; -*- coding: utf-8 -*-


(define tolerance 0.00001)

(define (fixed-point f first-guess)
  (define (close-enough? v1 v2) 
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (if (close-enough? guess next)
        next
        (try next))))
  (try first-guess))



;;Newton法
(define dx 0.00001)

(define (deriv g)
  (lambda (x) 
    (/ (- (g (+ x dx)) (g x)) 
       dx)))

(define (newton-transform g)
  (lambda (x) 
    (- x (/ (g x) ((deriv g) x)))))

(define (newtons-method g guess)
  (fixed-point (newton-transform g) guess))

;;
(define (cubic a b c)
  (lambda (x) 
    (+ (* x x x) (* a (* x x)) (* b x) c)))


;; main
(define (main args)


  (display "(newtons-method (cubic 4 5 6 ) 1) : ")
  (display (newtons-method (cubic 4 5 6 ) 1))
  (newline)(newline)


  (display "で、この答えを、((cubic 4 5 6) 答えの値) として実行 : ")
  (display ((cubic 4 5 6)
            (newtons-method (cubic 4 5 6 ) 1)))

 (newline)
0)


実行

(newtons-method (cubic 4 5 6 ) 1) : -3.0

で、この答えを、((cubic 4 5 6) 答えの値) として実行 : 0.0


できてるとおもう。たぶん。