[EMAIL PROTECTED]  writes: 
> 
> Here is a program:
> 
> import System
> import Monad
> import Char
> import Directory
> 
> main = do
>     let katalog = "/var/spool/news/articles/pl/rec/hihot"
>     nazwy <- getDirectoryContents katalog
>     let posty = map (\s -> katalog++'/':s) $ filter (all 
> isDigit) nazwy
>     r <- liftM concat $ mapM readFile $ posty
>     putStr r
>     exitWith ExitSuccess
> 
...
> 
> The first question is how to prevent Haskell from opening all the
> files before reading them?

Quite - your problem stems from the use of monad combinators:

     r <- liftM concat $ mapM readFile $ posty

mapM applies its first argument to all list elements of its second
arg before returning the result - i.e., you need to open all files
before you can start to print out the contents of the first one.
That's probably not what you want to do here..

To fix, I'd suggest either encoding the recursion explicitly or
defining a lazier version of mapM, e.g.,

  interleavedMapM :: (Monad m) => (a -> m b) -> [a] -> m [b]
  interleavedMapM f     [] = return []
  interleavedMapM f (x:xs) = do
      y  <- f x
      ys <- IOExts.unsafeInterleaveIO (interleavedMapM f xs)
      return (y:ys)


> Besides this, there must be a bug in ghc. It shouldn't SIGSEGV.

Can't say I disagree - trying to come up with a fix that gracefully
handles the situation. Thanks for the report.

--sigbjorn

Reply via email to