module Newton ( newton ) where next :: Double -> Double -> Double next m 0 = error "Division by zero" next m x = (x + m/x)/2 repeatedly :: (Double -> Double) -> Double -> [Double] repeatedly f a = a : repeatedly f (f a) relative :: Double -> [Double] -> Double relative tau (a:b:rest) | abs (a/b-1) <= tau = b | otherwise = relative tau (b:rest) newton x0 tau m = relative tau (repeatedly (next m) x0)