(defn average
"返回平均值"
[x y]
(/ (+ x y ) 2))
(defn improve-sqrt
"产生一个更好的猜测"
[x guess]
(average guess (/ x guess)))
(defn sqrt-stream
"牛顿法求平方根"
([x] (sqrt-stream x 1.0))
([x guess]
(lazy-seq (cons guess
(sqrt-stream x (improve-sqrt x guess))))))
(take 1000 (sqrt-stream 2))
Created
July 29, 2012 07:31
-
-
Save ngn999/3196518 to your computer and use it in GitHub Desktop.
用惰性求值来计算平方根
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment