Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment