SICP Exercise 1.44

Question

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-fold smoothed function. Show how to generate the $n$-fold smoothed function of any given function using smooth and repeated from exercise 1.43.

Answer

(define (square x) (* x x))

(define (compose f g)
  (λ (x) (f (g x))))

(define (repeated f n)
  (if (= n 1)
      (λ (x) (f x))
      (repeated (compose f f) (- n 1))))

(define (smooth f)
  (let ((dx 0.00001))
    (λ (x) (/ (+ (f x) (+ (f x) dx) (- (f x) dx)) 3))))

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

(sin 3.14152)
((n-fold-smooth sin 10) 3.14152)

Results:

7.265358972945281e-5
7.265355706761721e-5