[Haskell-cafe] Application of iterate

Neil Mitchell ndmitchell at gmail.com
Fri Mar 23 07:15:51 EDT 2007

Hi Longesh

> But I've found learning Haskell so tough (this is my 4th try in the last
> two years) that I feel I have to bore everyone with my first non-trivial
> program.

Well done on getting something going!

> my_sqrt t = last (take 20 (iterate (\n -> n/2 + t/(2 * n)) t))

last (take 20) can be reexpressed as:

> my_sqrt t = iterate (\n -> n/2 + t/(2 * n)) t !! 19

For information on what !! does (vs last and take), see Hoogle:

> 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.

close (x:y:xs) | abs (x - y) < 0.1 = y
close (x:xs) = close xs

I don't know how to add it into the one liner though - although I
suspect someone here will :)



More information about the Haskell-Cafe mailing list