Instantly share code, notes, and snippets.

@cdsmith /sqrt.hs
Last active Nov 6, 2018

Embed
What would you like to do?
fixpoint :: Eq a => (a -> a) -> a -> a
fixpoint f x
| x == fx = fx
| otherwise = fixpoint f fx
where fx = f x
mySqrt :: Double -> Double
mySqrt n = fixpoint (\x -> (x + n/x) / 2) 1
main :: IO ()
main = print (mySqrt 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment