Created
August 24, 2012 22:33
-
-
Save tazjin/3456506 to your computer and use it in GitHub Desktop.
Uses Haskell types to turn a String into garbage.
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.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