SICP : Ex1.44 : Concurrent Clean

smooth f x
    = (f (x-dx) + f x + f (x+dx)) / 3.0

nth_fold_smooth n
    = iter n smooth

これは、このように展開される(n=3の場合)

(smooth (smooth (smooth f))) x

動きを見るために、以下のテストコードを実行

dx :== 0.1 //テスト用に大き目の値を設定

smooth f x
    = (f (x-dx) + f x + f (x+dx)) / 3.0

nth_fold_smooth n
    = iter n smooth

Start
    = map (nth_fold_smooth n f) [-1.0,-0.9..1.0]
where
    n = 10
    f x | ~(dx/2.0) < x && x < (dx/2.0)
        = 1.0
        = 0.0

実行結果

-1.0 -> 1.69350878084303e-05
-0.9 -> 0.000169350878084303
-0.8 -> 0.000931429829463666
-0.7 -> 0.00355636843977036
-0.6 -> 0.0104150790021846
-0.5 -> 0.0245897474978408
-0.4 -> 0.0482650002540263
-0.3 -> 0.0802723162119596
-0.2 -> 0.114565869024031
-0.1 -> 0.141407983200393
0.0  -> 0.151619841148876
0.1  -> 0.141407983200393
0.2  -> 0.114565869024031
0.3  -> 0.0802723162119596
0.4  -> 0.0482650002540263
0.5  -> 0.0245897474978408
0.6  -> 0.0104150790021846
0.7  -> 0.00355636843977036
0.8  -> 0.000931429829463666
0.9  -> 0.000169350878084303
1.0  -> 1.69350878084303e-05