Created
May 2, 2014 02:26
-
-
Save ljsc/06f9bc66a3817a619352 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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