Personal tools

Haskell Quiz/Word Blender/Solution Sjanssen

From HaskellWiki

< Haskell Quiz | Word Blender
Revision as of 11:14, 13 January 2007 by Quale (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
import qualified Data.ByteString.Char8 as B
import System.Random
import Data.List
import Data.Char
 
-- Given sorted lists 'xs' and 'ys', 'subset xs ys' tests whether xs 
-- is a subset of ys
subset []     _      = True
subset _      []     = False
subset (x:xs) (y:ys) = case compare x y of
                            EQ -> subset xs ys
                            LT -> False
                            GT -> subset (x:xs) ys
 
valid w = B.length w >= 3 && B.length w <= 6 && B.all isAlpha w
 
uniq = map head . group
 
main = do
    f <- B.readFile "/usr/share/dict/words"
    let ws    = filter valid . B.lines . B.map toUpper $ f
        sixes = filter (\x -> B.length x == 6) ws
    i <- randomRIO (0, length sixes - 1)
    let seed = sort . B.unpack $ sixes !! i
 
    putStrLn "Letters:   "
    putStrLn seed
    putStrLn ""
 
    putStrLn "Solutions: "
    mapM_ B.putStrLn $ filter (\w -> subset (sort . B.unpack $ w) seed) ws