How to write a lazy variant ?

Dmitry Astapov adept@umc.com.ua
04 Dec 2001 16:28:59 +0200


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