Skip to content

Instantly share code, notes, and snippets.

@lattejed
Created June 2, 2013 16:16
Show Gist options
  • Save lattejed/5694003 to your computer and use it in GitHub Desktop.
Save lattejed/5694003 to your computer and use it in GitHub Desktop.
rotateR :: [a] -> [a]
rotateR [x] = [x]
rotateR xs = last xs : init xs
-- Make a pattern to match the consonants and vowels in a word where a cons is
-- not "aeiou" or y preceded by a cons e.g., "happy" -> ("happy", "cvccv")
wordDesc :: String -> (String, String)
wordDesc str =
(str, [corv (a,b,i) | (a,b,i) <- zip3 str (rotateR str) [0..len]])
where len = length str - 1
corv (a,b,i)
| a == 'y' && i /= 0 && b `notElem` vs = 'v'
| a `elem` vs = 'v'
| otherwise = 'c'
where vs = "aeiou"
-- Measure the number of consonant sequences in the word in the form
-- [c]vcvc[v] == 2 where the inner 'vc' sequences are counted.
measure :: (String, String) -> Int
measure (_,ds) =
length $ filter (=='c') ds'
where ds' = dropWhile (=='c') [head a | a <- group ds]
-- Tests if our word or stem ends with a given character
endswith :: (String, String) -> Char -> Bool
endswith (str,_) c = c == last str
-- Tests if our word or stem contains a vowel
hasvowel :: (String, String) -> Bool
hasvowel (_,ds) = 'v' `elem` ds
-- Tests if our word or stem ends with a double consonant
endsdblc :: (String, String) -> Bool
endsdblc (str,ds) =
last ds == 'c' && (last $ init str) == last str
-- Tests if our word or stem ends with the pattern 'cvc' and does
-- not end with the characters 'x', 'w' or 'y'
endscvc :: (String, String) -> Bool
endscvc (str,ds) =
drop (length ds - 3) ds == "cvc" && last str `notElem` "xwy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment