[Haskell-cafe] Memoization in Haskell?

Mike Dillon mike at embody.org
Fri Jul 9 11:51:31 EDT 2010


begin Edward Kmett quotation:
> The result is considerably faster:
> 
>     *Main> fastest_f 12380192300
>     67652175206
> 
>     *Main> fastest_f 12793129379123
>     120695231674999

I just thought I'd point out that running with these particular values
on a machine with a 32 bit Int will cause your machine to go deep into
swap... Anything constant greater that maxBound is being wrapped back to
the negative side, causing havoc to ensue. I changed the open version of
"f" to look like this to exclude negative values:

	f :: (Int -> Int) -> Int -> Int
	f mf 0             = 0
	f mf n | n < 0     = error $ "Invalid n value: " ++ show n
	f mf n | otherwise = max n $ mf (div n 2) +
								 mf (div n 3) +
								 mf (div n 4)

-md


More information about the Haskell-Cafe mailing list