Difference between revisions of "Haskell Quiz/Word Blender/Solution Sjanssen"

From HaskellWiki
Jump to navigation Jump to search
 
(+cat)
 
Line 1: Line 1:
  +
[[Category:Haskell Quiz solutions|Word Blender]]
 
<haskell>
 
<haskell>
 
import qualified Data.ByteString.Char8 as B
 
import qualified Data.ByteString.Char8 as B

Latest revision as of 11:14, 13 January 2007

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