Last active
October 5, 2017 22:45
-
-
Save NotTheEconomist/0b3ed35e140d000e3ce614d0ce5a327b to your computer and use it in GitHub Desktop.
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
import qualified Data.Set as Set | |
import Data.Char(toLower) | |
countChars :: String -> (Int, Int, Int) | |
countChars s = countChars' (0, 0, 0) (map toLower s) | |
where countChars' :: (Int, Int, Int) -> String -> (Int, Int, Int) | |
countChars' tup@(v, c, p) [] = tup | |
countChars' (v, c, p) (x:xs) | Set.member x vowels = countChars' (v+1, c, p) xs | |
| Set.member x consonants = countChars' (v, c+1, p) xs | |
| otherwise = countChars' (v, c, p+1) xs | |
vowels = Set.fromDistinctAscList "aeiou" | |
consonants = Set.difference (Set.fromDistinctAscList ['a'..'z']) vowels | |
countLines :: [String] -> (Int, Int, Int) | |
countLines xs = let addTriples (a, b, c) (a', b', c') = (a+a', b+b', c+c') | |
in foldr addTriples (0, 0, 0) $ map countChars xs | |
main :: IO () | |
main = do | |
putStr "Name of the file: " | |
filename <- getLine | |
contents <- readFile filename | |
let lines' = lines contents | |
(vowels, consonants, punctuation) = countLines lines' | |
putStrLn $ "vowels: " ++ (show vowels) | |
putStrLn $ "consonants: " ++ (show consonants) | |
putStrLn $ "punctuation: " ++ (show punctuation) |
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
Name of the file: test.txt | |
vowels: 20 | |
consonants: 39 | |
punctuation: 15 |
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
Hello, world! | |
This is just me, talking to myself, | |
writing a textfile | |
to read. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment