[Haskell-cafe] Comments and suggestions on code

Andre Nathan andre at digirati.com.br
Fri Jan 11 17:24:04 EST 2008


On Fri, 2008-01-11 at 20:20 -0200, Andre Nathan wrote:
> Both versions which use getDirectoryContents also use much more memory
> than the one which uses readDirStream (about 8M vs about 2M). Maybe I'm
> not exploting getDirectoryContents' laziness correctly? I expected the
> second and third versions to run in about the same time.

Forgot to paste the code...

foo :: IO ()
foo = do
  entries <- getDirectoryContents "."
  let procs = filter (=~ "^[0-9]+$") entries
  mapM_ putStrLn procs

processEntry :: DirStream -> IO ()
processEntry ds = do
  entry <- readDirStream ds
  if entry =~ "^[0-9]+$"
    then do
      putStrLn entry
      processEntry ds
    else
      if entry == "" then return () else processEntry ds

bar :: IO ()
bar = do
  ds <- openDirStream "."
  processEntry ds
  closeDirStream ds

processEntry' :: FilePath -> IO ()
processEntry' entry = do
  if entry =~ "^[0-9]+$"
    then putStrLn entry
    else return ()

baz :: IO ()
baz = do
  entries <- getDirectoryContents "."
  mapM_ processEntry' entries

main = forM_ [1..1000] $ \_ -> foo {- bar -} {- baz -}



More information about the Haskell-Cafe mailing list