
On Sun, May 16, 2010 at 11:03:03PM +1200, Stephen Blackheath [to Haskell-Beginners] wrote:
Andrew,
On 15/05/10 11:57, Andrew Sackville-West wrote:
I'm having trouble determining how to put this into the existing context of a string of filter's and maps where the contents of the file are used in a predicate to a filter. (if you really want you can look at my ridiculous code at http://git.swclan.homelinux.org/rss2email.git)
I took a look. You've got a list of items and you want to check each one against your 'seen it' file. I'm not sure what your requirements are but currently the whole file gets read into memory. So, sticking with that, here's _a_ way to do it (with a Set, which gives a faster lookup):
yeah, reading it all in is fine. NOt sure in the long term what the problems are with that. I suppose if it was a really big history file, it would be important to do something else, but it works for now.
import Control.Exception import Data.Set (Set) import qualified Data.Set as S import System.IO.Error import Prelude hiding (catch)
-- | Return "seen it" predicate readHistory :: FilePath -> IO (String -> Bool) readHistory fn = do hist <- withFile fn ReadMode $ \h -> fetchLines h S.empty return (`S.member` hist) where fetchLines h hist = do l <- hGetLine h fetchLines h $! S.insert l hist `catch` \exc -> if isEOFError exc then return hist else throwIO exc
This is completely strict. The $! is there to make sure we're keeping a set in memory, not a chain of inserts (though the inserts wouldn't actually take up any more memory than the set does). I haven't tried compiling this.
thanks for this. it helps a lot. hmmm... I wonder why it is I never have a problem returning functions in Scheme, but it never occurs to me as I learn Haskell? thanks for your help. A