HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Haskell Quiz/Phone Number Words/Solution Jethr0

< Haskell Quiz | Phone Number Words

Categories: Haskell Quiz solutions


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

Retrieved from "http://www.haskell.org/haskellwiki/Haskell_Quiz/Phone_Number_Words/Solution_Jethr0"

This page has been accessed 704 times. This page was last modified 10:51, 13 January 2007. Recent content is available under a simple permissive license.