Haskell Quiz/Phone Number Words/Solution Jethr0

From HaskellWiki
< Haskell Quiz‎ | Phone Number Words
Revision as of 17:48, 15 December 2006 by JohannesAhlmann (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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) . 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