
Consider the following functions: readRecFile fname = do ifh <- openFile fname ReadMode readRecords ifh readRecords ifh = do x <- decodeNextRecord ifh more <- isEof ifh if (more == False) then return [x] else do rest <- readRecords ifh return (x:rest) They allow me to read contest of file fname into a list of data structures. But "do rest <- readRecords ifh ; return (x:rest)" part makes them behave non-lazily. Maybe I'm just too tired right now, but it escapes me how can I write a lazy variant of those functions, so that they will behave like getContents. Does anyone have a clue? -- Dmitry Astapov //ADEpt E-mail: adept@umc.com.ua GPG KeyID/fprint: F5D7639D/CA36 E6C4 815D 434D 0498 2B08 7867 4860 F5D7 639D

DA> Consider the following functions: DA> readRecFile fname = DA> do ifh <- openFile fname ReadMode DA> readRecords ifh DA> readRecords ifh = DA> do x <- decodeNextRecord ifh DA> more <- isEof ifh DA> if (more == False) DA> then return [x] DA> else do rest <- readRecords ifh DA> return (x:rest) DA> They allow me to read contest of file fname into a list of data DA> structures. But "do rest <- readRecords ifh ; return (x:rest)" part makes DA> them behave non-lazily. Maybe I'm just too tired right now, but it escapes DA> me how can I write a lazy variant of those functions, so that they will DA> behave like getContents. Does anyone have a clue? I hate to followup to myself, but looks like I had to. I really was too tired and lazy version could be obtained by simple use of accumulator:
readRecFile fname = do ifh <- openFile fname ReadMode readRecords ifh []
readRecords ifh acc = do x <- decodeNextRecord ifh more <- isEof ifh if (more == False) then return (x:acc) else do readRecords ifh (x:acc)
That's it. Sorry for noice on the list. -- Dmitry Astapov //ADEpt E-mail: adept@umc.com.ua GPG KeyID/fprint: F5D7639D/CA36 E6C4 815D 434D 0498 2B08 7867 4860 F5D7 639D

I hate to followup to myself, but looks like I had to. I really was too tired and lazy version could be obtained by simple use of accumulator:
Are you _sure_ that's all it takes? I was expecting that you'd need 'unsafeInterleaveIO'. (This function is used to implement hGetContents. Some info about it shows up under google and in john launchbury and simon peyton jones' paper on lazy functional state threads (section 7.2)). -- Alastair Reid reid@cs.utah.edu http://www.cs.utah.edu/~reid/

I hate to followup to myself, but looks like I had to. I really was too tired and lazy version could be obtained by simple use of accumulator: ADR> Are you _sure_ that's all it takes?
Well, I cannot be 100% sure, but what I observe in ghci is that evaluating 'recs <- readRecordFile "some_name"' with first variant of routines takes about two minutes to complete (file is quite large) and gobbles up substatial amount of memory, while second variant returns almost immediately, and consumes memory only when I start to process recs. -- Dmitry Astapov //ADEpt E-mail: adept@umc.com.ua GPG KeyID/fprint: F5D7639D/CA36 E6C4 815D 434D 0498 2B08 7867 4860 F5D7 639D
participants (2)
-
Alastair David Reid
-
Dmitry Astapov