Exercise1.40~1.46

Exercise1.40

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

実行例
gosh> (newtons-method (cubic -13 52 -60) 1)
2.0000000000037463
by chrono

Exercise 1.41

(define (double func)
  (lambda (x) (func (func x))))

実行例
gosh> (((double (double double)) inc) 5)
21
by iwk

Exercise 1.42

(define (compose f g)
  (lambda (x) (f (g x))))
by iwk

Exercise 1.43

<ヒントに従ってExercise1.42のcomposeを使う>
(define (repeated f n)
  (if (= n 1)
      f
      (compose f (repeated f (- n 1)))))
by chrono

<追加: 反復的プロセス版>
(define (repeated f n)
  (define (iter a n)
    (if (= n 1)
        a
        (iter (compose f a) (- n 1))))
  (iter f n))
by kacchi

<追加2: accumulate使用>
;; Exercise1.32のaccumulateを使用する
(define (repeated f n)
  (accumulate compose identity (const f) 1 inc n))
by alaskan

Exercise1.44

(define (smooth f)
  (let ((dx 0.1))
    (lambda (x)
      (/ (+ (f (- x dx))  (f x)  (f (+ x dx)))
         3.0))))

;;; n-fold smoothed function
(define (n-smooth f n)
  (lambda (x)
    ((repeated (smooth f) n) x)))
by chrono

Exercise1.45

(define (n-th-root n)
  (lambda (x)
    (fixed-point-of-transform
     (lambda (y) (/. x (fast-expt y (- n 1))))
     (repeated average-damp
               (floor->exact (/ (log n) (log 2))))
     1.0)))
by alaskan

Exercise1.46

(define (iterative-improve is-answer? improve-pred)
  (define (improve p)
    (if (is-answer? p) p
	(improve (improve-pred p))))
  improve)

(define (sqrt x)
  (define (good-enough? p)
    (< (abs (- (square p) x)) 0.001))
  (define (improve-guess guess)
    (average guess (/ x guess)))
  ((iterative-improve good-enough? improve-guess) 1.0))

(define (fixed-point f first-guess)
  (define (good-enough? p)
    (< (abs (- (f p) p)) 0.001))
  ((iterative-improve good-enough? f) first-guess))
by chrono

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2010年01月07日 00:23
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。