Last active
March 12, 2024 09:11
-
-
Save aflaag/df69d59f419091ef97cca37f817f260f to your computer and use it in GitHub Desktop.
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
import Data.Char (toLower) | |
import Data.List (sort) | |
isAlpha c = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') | |
lower = map (\w -> toLower w) | |
count t = length . filter (== t) | |
removeDups [] = [] | |
removeDups (x:xs) = x : (filter (/= x) $ removeDups xs) | |
join sep [] = "" | |
join sep (x:xs) = x ++ sep ++ (join sep xs) | |
format sep = map (\x -> snd x ++ sep ++ (show $ fst x)) | |
commonWords n cs = join "\n" $ format " : " $ take n . reverse . sort . removeDups $ zip (map (\w -> count w ws) ws) ws | |
where ws = map (lower . filter isAlpha) $ words cs | |
main :: IO () | |
main = do putStrLn $ show $ commonWords 3 "This, indeed, is a test input! Test input I said." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment