[Haskell-cafe] Application of iterate

John Meacham john at repetae.net
Fri Mar 23 17:26:54 EDT 2007


On Fri, Mar 23, 2007 at 01:04:47PM +0200, Logesh Pillay wrote:
> my_sqrt t = last (take 20 (iterate (\n -> n/2 + t/(2 * n)) t))
> 
> It is a bit crude though.  20 iterations is a bit arbitrary. I don't suppose
> there is a easy way to iterate until the results stop changing.
> 

perhaps
> sqrt t = head [ n | (n,True) <- iterate (\n -> let n' = n/2 + t/(2 * n) in (n',n' == n)) t ]

        John

-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell-Cafe mailing list