[Haskell-beginners] Re: [Haskell-begin] Maximum of a List?

Rafael Gustavo da Cunha Pereira Pinto rafaelgcpp at gmail.com
Mon Jul 28 19:53:07 EDT 2008


Since this is the "beginners" list, could someone explain me why using g
made everything run like the wind, with almost no memory?

I am puzzled! :-(

On Mon, Jul 28, 2008 at 17:32, Steve Klabnik <steve.klabnik at gmail.com>wrote:

>
>
> Finally, what I ended up doing:
>
> f :: Integer -> Integer -> Integer
> f acc x
>     | x == 1 = acc
>         | even x = f (acc + 1) (x `div` 2)
>         | otherwise = f (acc + 1) (3 * x + 1)
>
> g :: Integer -> (Integer, Integer)
> g x = (f 1 x, x)
>
> answer = (foldl' max (0,0)) $ map g [1 .. 999999]
>
>
> main = putStrLn( show answer)
>
>
>


-- 
Rafael Gustavo da Cunha Pereira Pinto
Electronic Engineer, MSc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20080728/b7cdf249/attachment.htm


More information about the Beginners mailing list