Skip to content

Instantly share code, notes, and snippets.

@ljsc
Created May 2, 2014 02:26
Show Gist options
  • Save ljsc/06f9bc66a3817a619352 to your computer and use it in GitHub Desktop.
Save ljsc/06f9bc66a3817a619352 to your computer and use it in GitHub Desktop.
;; take your make-guess function
(defn make-guess [guess]
(/ (+ (/ 2.0 guess) guess) 2.0))
;; and iterate to a lazy-seq of approximations
(def guesses (iterate make-guess 1.0))
;; then we can zip the list with itself, but shifted one value
;; this gives us a seq of guess pairs.
(def guess-pairs
(map list guesses (rest guesses)))
;; we can stop when they reach a fixed point. Then take one value
;; from that pair.
(->> guess-pairs
(drop-while (fn [[x y]] (not= x y)))
first
first)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment