Guest User

Untitled

a guest
Dec 10th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import Data.List (sort)
  2.  
  3. -- |Type which holds a single character depending
  4. -- on what it is.
  5. data CharType = Consonant Char
  6.  | Vowel Char
  7. | Other Char
  8. deriving (Eq)
  9.  
  10. instance Show CharType where
  11. show (Consonant c) = c : []
  12. show (Vowel c) = c : []
  13. show (Other c) = c : []
  14.  
  15. instance Ord CharType where
  16. compare (Consonant _) (Consonant _) = EQ
  17. compare (Consonant _) _ = LT
  18. compare (Vowel _) (Vowel _) = EQ
  19. compare (Vowel _) (Consonant _) = GT
  20. compare (Vowel _) (Other _) = LT
  21. compare (Other _) (Other _) = EQ
  22. compare (Other _) _ = LT
  23.  
  24. -- |Wrapper around a list of 'CharType'@s@ for convenient
  25. -- handling of entire words.
  26. newtype Word = Word {unWord :: [CharType]}
  27.  
  28. instance Show Word where
  29. show = concat . map show . unWord
  30.  
  31.  
  32. -- |Wrapper around a list of 'Word'@s@ for convenient
  33. -- handling of entire sentences.
  34. newtype Sentence = Sentence { unSentence :: [Word]}
  35.  
  36. instance Show Sentence where
  37. show = unwords . map show . unSentence
  38.  
  39. -- |\"Smart\" constructor for 'CharType'
  40. charType :: Char -> CharType
  41. charType c | c `elem` vowels = Vowel c
  42. | c `elem` consonants = Consonant c
  43. | otherwise = Other c
  44. where
  45. vowels = "aeiouAEIOUäüöÄÜÖåÅøØÆ挜"
  46. consonants = filter (not .flip elem vowels) (['a'..'z'] ++ ['A'..'Z'])
  47.  
  48. -- |Turns a single 'String' into a 'Word'. It must be a single word.
  49. mkWord :: String -> Word
  50. mkWord = Word . sort. map charType
  51.  
  52. -- |Watifies a 'String' which contains an entire sentence.
  53. mkSentence :: String -> Sentence
  54. mkSentence = Sentence . map mkWord . words
Add Comment
Please, Sign In to add comment