Tuesday, September 01, 2020

SICP in Clojure: Chapter 1, Exercise 39

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

 (ns sicp.ch1.ex39
  (:require [sicp.ch1.ex16 :as ch1-ex16]
            [sicp.ch1.ex37 :as ch1-ex37]))

(defn tan-cf
  [x k]
  (/
   (ch1-ex37/cont-frac (constantly (- (ch1-ex16/square x)))
                       #(inc (* 2 (dec %)))
                       k)
   (- x)))

(comment
  (double (tan-cf (/ Math/PI 6) 10)) ; => 0.5773502691896257
  #__)