[Haskell-cafe] Fusion for fun and profi (Was: newbie optimization question)

Isaac Dupree isaacdupree at charter.net
Mon Oct 29 09:50:21 EDT 2007

Don Stewart wrote:
>     default(Int)
>     divisors i   = filter (\j -> i `rem`j == 0)         (enumFromTo 1 (i-1))
>     main = print $ filter (\i -> i == sum (divisors i)) (enumFromTo 1 10000)


> So almost identical types to the C program (bar for the return [Int]). 
> Finally, we can manually translate the C code into a confusing set of nested
> loops with interleaved IO,

how lazy is `print` supposed to be?  If it's strict, we need to return / 
accumulate a list (and in this case, that is not very time-consuming 
because there are only four numbers in the list)

from http://haskell.org/onlinereport/standard-prelude.html

print x    =  putStrLn (show x)

show on lists is lazy and doesn't matter that it's not for Ints

putStrLn   :: String -> IO ()
putStrLn s =  do putStr s
                  putStr "\n"

putStr     :: String -> IO ()
putStr s   =  mapM_ putChar s

okay, mapM_ makes it lazy.

putChar    :: Char -> IO ()
putChar    =  primPutChar

(It's easy to get confused with OS buffering effects too...)

so it should ALL be able to fuse, with no intermediate lists, I think 
(not sure about [Char] from show... or whether fusion works with IO 


More information about the Haskell-Cafe mailing list