Skip to content

Instantly share code, notes, and snippets.

@roehst
Created November 13, 2016 20:14
Show Gist options
  • Save roehst/19b92f87fe1f3acbacb8b276e66ee337 to your computer and use it in GitHub Desktop.
Save roehst/19b92f87fe1f3acbacb8b276e66ee337 to your computer and use it in GitHub Desktop.
Traduzor para Catiorineoes
import Data.List (find, isPrefixOf)
data Syllab = Syllab { findStr :: String, replaceStr :: String }
deriving Show
appliesTo string syllab = isPrefixOf (findStr syllab) string
vocabulary = [Syllab "cho" "tio",
Syllab "nho" "neo",
Syllab "rr" "r",
Syllab "mul" "mol",
Syllab "lhe" "lie",
Syllab "nh" "ni"]
step :: [Syllab] -> (String, String) -> (String, String)
step syllabs (current, remaining) =
case find (appliesTo remaining) syllabs of
Nothing ->
let current' = current ++ [head remaining] in
let remaining' = tail remaining in
(current', remaining')
Just syllab ->
let current' = current in -- current ++ (replaceStr syllab) in
let remaining' = (replaceStr syllab) ++ (drop (length (findStr syllab)) remaining) in
(current', remaining')
translate :: [Syllab] -> String -> String
translate syllabs string =
translate' syllabs ("", string) where
translate' syllabs (cur, rem) =
if rem == "" then
cur
else
translate' syllabs (step syllabs (cur, rem))
main = do
(fmap (translate vocabulary) getLine) >>= print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment