Skip to content

Instantly share code, notes, and snippets.

@jartur
Created July 20, 2012 13:27
Show Gist options
  • Save jartur/3150711 to your computer and use it in GitHub Desktop.
Save jartur/3150711 to your computer and use it in GitHub Desktop.
Memory hog + Stack overflow
module Main where
import System.IO
import qualified Data.Map as M
main = do
inh <- openFile "text.txt" ReadMode
outh <- openFile "dict.txt" WriteMode
counts <- mainLoop inh M.empty
let excsPairs = M.assocs counts
let excsStr = concat $ map (\(w, count) -> w ++ " :: " ++ (show count) ++ "\n") excsPairs
hPutStr outh excsStr
hClose inh
hClose outh
mainLoop :: Handle -> M.Map String Int -> IO (M.Map String Int)
mainLoop inh counts = do
inEof <- hIsEOF inh
if inEof
then return counts
else do
ws <- (hGetLine inh >>= \inStr -> return $ words inStr)
mainLoop inh $ updateMapForWords ws counts
updateMapForWords :: [String] -> M.Map String Int -> M.Map String Int
updateMapForWords ws counts =
foldl (\m w -> M.alter updateCount w m) counts ws
where
updateCount (Just c) = Just $ c + 1
updateCount Nothing = Just 1
@applicative
Copy link

The fork I just made https://gist.github.com/3151613 is how I would have written this if I were just writing from scratch; it may be you were taking a more complex approach because you were simplifying a more complex case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment