Sunday, December 27, 2020

SICP in Clojure: Chapter 1, Exercise 44

This is my Clojure solution to Chapter 1, Exercise 44:

(ns sicp.ch1.ex44
  (:require [sicp.ch1.ex16 :as ch1-ex16]
            [sicp.ch1.ex40 :as ch1-ex40]
            [sicp.ch1.ex43 :as ch1-ex43]))

(defn smooth
  [f]
  #(/ (+ (f (- % ch1-ex40/dx))
         (f %)
         (f (+ % ch1-ex40/dx)))
      3))

(defn n-fold-smooth
  [f n]
  ((ch1-ex43/repeated smooth n) f))

(comment
  ((smooth ch1-ex16/square) 5) ; => ~25

  ((n-fold-smooth ch1-ex16/square 2) 5) ; => ~25
  #__)

 

No comments: