sicp每日一題[1.44]

再思即可發表於2024-09-04

Exercise 1.44

The idea of smoothing a function is an important concept in signal processing. If f is a function and dx is some small number, then the smoothed version of f is the function whose value at a point x is the average of f(x-dx), f(x), and f(x+dx).Write a procedure smooth that takes as input a procedure that computes f and returns a procedure that computes the smoothed f . It is sometimes valuable to repeatedly smooth a function (that is, smooth the smoothed function, and so on) to obtain the n-folds moothed function. Show how to generate the n-fold smoothed function of any given function using smooth and repeated from Exercise 1.43.


這道題難度不大,smooth 函式返回一個計算3個數平均值的 lambda 函式就行。

(define dx 0.0001)
(define (smooth f)
  (lambda (x) (/ (+ (f (- x dx))
                    (f x)
                    (f (+ x dx)))
                 3)))

(define (n-fold-smooth f n)
  (repeated f n))


((smooth square) 5)
((smooth inc) 5)

((n-fold-smooth square 2) 5)
((n-fold-smooth inc 10) 5)

; 執行結果
25.000000006666667
6.0

625
15

相關文章