Last active
August 29, 2015 14:01
-
-
Save remusao/213cabad5ecd00da8e82 to your computer and use it in GitHub Desktop.
Wordcount in Haskell
This file contains 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 Data.List | |
import qualified Data.Text.Lazy.IO as TIO | |
import qualified Data.Text.Lazy as T | |
import qualified Data.Map.Strict as M | |
import System.Environment | |
import Control.Monad | |
type Str = T.Text | |
wordcount :: Str -> M.Map Str Int | |
wordcount str = foldl' (flip $ M.alter incr) M.empty (T.words str) | |
where | |
incr Nothing = Just 1 | |
incr (Just count) = Just (count + 1) | |
main :: IO () | |
main = do | |
args <- getArgs | |
when (length args == 1) $ do | |
content <- TIO.readFile $ head args | |
print $ M.size $ wordcount content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment