[Haskell-beginners] Performance of Idiomatic lazy Haskell

Stephen Tetley stephen.tetley at gmail.com
Sun Jan 31 07:23:33 EST 2010


Hi Markus

Whoops, I hadn't read your email properly and wasn't accounting for
the epsilon. Here's a version that does, although it is perhaps a
little slow...

> import Data.List (unfoldr)


> leibniz eps = converge eps ser


> ser :: [Double]
> ser = unfoldr phi (True,1) where
>   phi (sig,d) | sig == True = Just (1/d, (False,d+2))
>               | otherwise   = Just (negate (1/d), (True,d+2))


> converge :: Double -> [Double] -> Double
> converge eps xs = step 0 0 xs where
>   step a b (x:xs) = let a' = a + (4*x) in
>                     if abs (a'-b) < eps then a' else step a' a xs

> demo1 =  leibniz 0.00000000025

Best wishes

Stephen


More information about the Beginners mailing list