Skip to content

Instantly share code, notes, and snippets.

@aflaag
Last active March 12, 2024 09:11
Show Gist options
  • Save aflaag/df69d59f419091ef97cca37f817f260f to your computer and use it in GitHub Desktop.
Save aflaag/df69d59f419091ef97cca37f817f260f to your computer and use it in GitHub Desktop.
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