Skip to content

Instantly share code, notes, and snippets.

@etscrivner
Created July 14, 2015 05:36
Show Gist options
  • Select an option

  • Save etscrivner/bc907c63d7303fe32def to your computer and use it in GitHub Desktop.

Select an option

Save etscrivner/bc907c63d7303fe32def to your computer and use it in GitHub Desktop.
Haskell lazy evaluated sqrt approximations
average :: Float -> Float -> Float
average x y = (x + y) / 2
sqrtImprove :: Float -> Float -> Float
sqrtImprove guess x = average guess (x / guess)
sqrtStream :: Float -> [Float]
sqrtStream x = 1.0 : (map (\guess -> (sqrtImprove guess x)) (sqrtStream x))
main :: IO ()
main = print (take 5 (sqrtStream 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment