[Haskell-cafe] Application of iterate
lemming at henning-thielemann.de
Fri Mar 23 10:37:21 EDT 2007
On Fri, 23 Mar 2007, Neil Mitchell wrote:
> 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))
inverse t (\x->(x^2,2*x)) 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.
> 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 :)
limitDifference :: (Real a) => a -> [a] -> a
limitDifference tol xs =
case dropWhile ((> tol) . abs . uncurry (-)) $ zip xs (tail xs) of
((_,x):_) -> x
 -> error "limitDifference: Finite sequence, but no element satisfies abort criterion."
More information about the Haskell-Cafe