Haskell Quiz/Phone Number Words/Solution Jethr0: Difference between revisions
mNo edit summary |
sharpen cat |
||
Line 1: | Line 1: | ||
[[Category: | [[Category:Haskell Quiz solutions|Phone Number Words]] | ||
<haskell> | <haskell> |
Revision as of 10:51, 13 January 2007
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) . sequence . map poss $ xs
poss char = encodings!!(read [char] :: Int)
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 "-") . sequence . map allPoss . splitThem