Exercise1-43 <---> Exercise1-45
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 obtained 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.
Scheme solution:
(define (smooth f)
(define dx .00001)
(lambda (x) (/ (+ (f (- x dx)) (f x) (f (+ x dx))) 3.0)))
(define (n-fold-smooth f n)
((repeated smooth n) f))
Haskell solution:
smooth f = \x -> (f (x-dx) + f x + f (x+dx)) / 3
where dx = 0.00001
n_fold_smooth f n = repeated smooth n f
OCaml solution:
let smooth f =
let dx = 0.00001 in
fun x -> (f (x -. dx) +. f x +. f (x +. dx)) /. 3.
let n_fold_smooth f n = repeated smooth n f
Standard ML solution:
fun smooth f = let
val dx = 0.00001
in
fn x => (f (x - dx) + f x + f (x + dx)) / 3.0
end
fun n_fold_smooth (f, n) = repeated (smooth, n) f
Exercise1-43 <---> Exercise1-45