Skip to content

Instantly share code, notes, and snippets.

@tazjin
Created August 24, 2012 22:33
Show Gist options
  • Select an option

  • Save tazjin/3456506 to your computer and use it in GitHub Desktop.

Select an option

Save tazjin/3456506 to your computer and use it in GitHub Desktop.
Uses Haskell types to turn a String into garbage.
import Data.List (sort)
-- |Type which holds a single character depending
-- on what it is.
data CharType = Consonant Char
 | Vowel Char
| Other Char
deriving (Eq)
instance Show CharType where
show (Consonant c) = c : []
show (Vowel c) = c : []
show (Other c) = c : []
instance Ord CharType where
compare (Consonant _) (Consonant _) = EQ
compare (Consonant _) _ = LT
compare (Vowel _) (Vowel _) = EQ
compare (Vowel _) (Consonant _) = GT
compare (Vowel _) (Other _) = LT
compare (Other _) (Other _) = EQ
compare (Other _) _ = LT
-- |Wrapper around a list of 'CharType'@s@ for convenient
-- handling of entire words.
newtype Word = Word {unWord :: [CharType]}
instance Show Word where
show = concat . map show . unWord
-- |Wrapper around a list of 'Word'@s@ for convenient
-- handling of entire sentences.
newtype Sentence = Sentence { unSentence :: [Word]}
instance Show Sentence where
show = unwords . map show . unSentence
-- |\"Smart\" constructor for 'CharType'
charType :: Char -> CharType
charType c | c `elem` vowels = Vowel c
| c `elem` consonants = Consonant c
| otherwise = Other c
where
vowels = "aeiouAEIOUäüöÄÜÖåÅøØÆæŒœ"
consonants = filter (not .flip elem vowels) (['a'..'z'] ++ ['A'..'Z'])
-- |Turns a single 'String' into a 'Word'. It must be a single word.
mkWord :: String -> Word
mkWord = Word . sort. map charType
-- |Watifies a 'String' which contains an entire sentence.
mkSentence :: String -> Sentence
mkSentence = Sentence . map mkWord . words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment