Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: lazy IO in readFile
(Stephen Blackheath [to Haskell-Beginners])
2. Re: Help with slow algorithm (Heinrich Apfelmus)
----------------------------------------------------------------------
Message: 1
Date: Sun, 16 May 2010 23:03:03 +1200
From: "Stephen Blackheath [to Haskell-Beginners]"
<[email protected]>
Subject: Re: [Haskell-beginners] lazy IO in readFile
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
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):
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.
Steve
------------------------------
Message: 2
Date: Sun, 16 May 2010 16:33:25 +0200
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: Help with slow algorithm
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Daniel Fischer wrote:
> Heinrich Apfelmus wrote:
>> Diego Echeverri wrote:
>>> I believe that the bottleneck is in the addOne function (Specially in
>>> the case where you have a bunch of nines). I have rewritten it to use
>>> STArray but isn't fast enough.
>>> doing: addOne $ B.replicate 500000 'a' takes too much time!
>>
>> Are you sure that you are using a fast algorithm? For instance, starting
>> with k and counting up until you reach a palindrome is definitely not a
>> good idea; you need to do something cleverer.
>>
>
> Deceived by a name :)
>
> He did something cleverer. His algorithm is good, just the implementation
> leaves much room for improvement.
Oops, my bad. :-$
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 23, Issue 22
*****************************************