Haskell Quiz/Phone Number Words/Solution Jethr0
Jump to navigation
Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
dictionary = map (map toUpper) ["haskell", "join", "the", "quiz"]
-- fetch all possible words represented by a string of consecutive digits
-- the results are checked against the dictionary
-- if no result is found, the original string is returned as possibility
allPoss xs = if null result then [xs] else result
where result = filter (`elem` dictionary) . mapM poss $ xs
poss char = encodings!! digitToInt char
encodings = ["0", "1", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"]
splitThem [] = []
splitThem xs = y : rest
where (y,ys) = break (not . isDigit) xs
rest = splitThem . dropWhile (not . isDigit) $ ys
-- > phoneNames "1.800.5646.843.4275355.7849"
-- ["1-800-JOIN-THE-HASKELL-QUIZ"]
phoneNames = map (concat . intersperse "-") . mapM allPoss . splitThem