I want to count the number of occurrences of each character in a large file. Although I know that counting should be implemented in a strict way in Haskell (which I tried to achieve using foldl'), I am still running out of memory. For comparison: the file size is about 2GB, while the computer has 100GB of memory. There are not many different characters in that file - maybe 20. What am I doing wrong?
ins :: [(Char,Int)] -> Char -> [(Char,Int)]
ins [] c = [(c,1)]
ins ((c,i):cs) d
| c == d = (c,i+1):cs
| otherwise = (c,i) : ins cs d
main = do
[file] <- getArgs
txt <- readFile file
print $ foldl' ins [] txt