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