Created
July 20, 2012 13:27
-
-
Save jartur/3150711 to your computer and use it in GitHub Desktop.
Memory hog + Stack overflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.