The x+1 looks suspicious.

On Sep 4, 2006, at 23:03 , John Goerzen wrote:

I have the below program, and I'm trying to run it on an input of about
90MB.  It eats RAM like crazy, and I can't figure out why.

I do know that the problem is not my custwords function (as you can see, I replaced the call to it with a call to the standard words function on
the last line).  It seems to be wordfreq, but I don't know why.

For information about the problem, see
http://changelog.complete.org/posts/535-A-Haskell-solution-to-Lars- Problem.html

Here's the code:

import System.Environment
import Data.List
import Data.Char
import qualified Data.Map as Map

custwords = filter (/= "") . lines . map (conv . toLower)
    where iswordchar x = isAlphaNum x && isAscii x
          conv x = if iswordchar x then x else '\n'

wordfreq inp = Map.toList $ foldl' updatemap (Map.empty::Map.Map String Int) inp
    where updatemap nm word = Map.insertWith updatefunc word 1 nm
          updatefunc _ x = x + 1

freqsort (w1, c1) (w2, c2) = if c1 == c2
                                 then compare w1 w2
                                 else compare c2 c1

showit (word, count) = show count ++ " " ++ word
main = do args <- getArgs
interact $ unlines . map showit . take (read . head $ args) .
                     sortBy freqsort . wordfreq . words


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to