[Haskell-cafe] Re: Haskell version of Norvig's Python Spelling Corrector

Derek Elkins derek.a.elkins at gmail.com
Sun Apr 22 16:07:13 EDT 2007


Pete Kazmier wrote:
> Derek Elkins <derek.a.elkins at gmail.com> writes:
> 
>>> After reading Bryan's post, I switched my right fold to a strict left
>>> fold and almost tripled my original speed.  Could someone provide some
>>> guidelines on when to use each?  I thought foldr should be used when
>>> building some large data structure such as the map I build.
>>> Bryan, out of curiosity, is a non bytestring version of your code
>>> faster?
>> http://www.haskell.org/hawiki/StackOverflow
> 
> I read the article and understand it, but I am having a hard time
> applying that specifically to my use of foldr.  Here is how I was
> using foldr:
> 
>   type WordFreq   = M.Map B.ByteString Int
>  
>   train:: [B.ByteString] -> WordFreq
>   train words = frequencyMap
>       where
>         frequencyMap     = foldr incWordCount M.empty words
>         incWordCount w m = M.insertWith (+) w 1 m
> 
> So is 'incWordCount' strict in its second argument?  I'm still not
> sure exactly what that means.  According to the wiki page, if it is
> strict in the second argument, I should have used foldl' instead of
> foldr.

insertWith will definitely need to examine 'm' to perform it's action, therefore 
it is strict.


More information about the Haskell-Cafe mailing list