This is my Clojure solution to Chapter 1, Exercise 40:
(ns sicp.ch1.ex40
(:require [sicp.ch1.ex15 :as ch1-ex15]
[sicp.ch1.ex16 :as ch1-ex16]
[sicp.ch1.ex35 :as ch1-ex35]))
(def dx 0.00001)
(defn deriv
[f]
#(/ (- (f (+ % dx)) (f %)) dx))
(defn newton-transform
[f]
#(- % (/ (f %) ((deriv f) %))))
(defn newtons-method
[f guess]
(ch1-ex35/fixed-point (newton-transform f) guess))
(defn cubic
[a b c]
#(+ (ch1-ex15/cube %)
(* a (ch1-ex16/square %) (* b %) c)))
(comment
(< ((cubic 1 2 3) (newtons-method (cubic 1 2 3) 1.0)) dx)
#__)
No comments:
Post a Comment