Skip to content

Instantly share code, notes, and snippets.

@Zoybean
Forked from Qata/Pronounify.hs
Last active February 5, 2017 04:59
Show Gist options
  • Save Zoybean/add7b2d9ad41feeb371e546514e2deaf to your computer and use it in GitHub Desktop.
Save Zoybean/add7b2d9ad41feeb371e546514e2deaf to your computer and use it in GitHub Desktop.
import Data.String.Utils (replace)
import Data.Char (isUpper, toUpper)
import System.Environment (getArgs)
main :: IO ()
main = do
strings <- getArgs
mapM_ (putStrLn . pronounify) strings
capitalise :: String -> String
capitalise (x:xs) = (toUpper $ x) : xs
capitalise "" = ""
isCapitalised :: String -> Bool
isCapitalised (x:_) = isUpper x
isCapitalised "" = False
forEachPronoun :: String -> String -> [String]
forEachPronoun x y =
let
orify a b = a ++ " or " ++ b
commaify a b = a ++ ", " ++ b
orifiedPronouns = [orify x y, orify y x]
commaedPronouns = [commaify x y, commaify y x]
combinedPronouns = orifiedPronouns ++ commaedPronouns
in
combinedPronouns ++ map capitalise combinedPronouns
pronouns =
map (\((s, h), t) -> (forEachPronoun s h, t))
[ (("she", "he"), "they")
, (("her", "him"), "them")
, (("her", "his"), "their")
, (("hers", "his"), "theirs")
, (("herself", "himself"), "themself")
]
pronounify :: String -> String
pronounify s =
let
replacementF r s p
| isCapitalised p = replace p (capitalise r) s
| otherwise = replace p r s
replacePronoun (ps, r) s = foldl (replacementF r) s ps
in
foldl (flip replacePronoun) s pronouns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment