Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/env stack
- {-
- stack
- script
- --resolver lts-10.3
- --ghc-build tinfo6
- -}
- {-# LANGUAGE LambdaCase #-}
- module Lib where
- import System.Random
- import Data.Array.IO
- import Control.Monad
- import Data.List.Split
- import Text.Read (readMaybe)
- import Data.Maybe
- lists :: [(String, String)]
- lists = [("wl_8.txt", "aoeuhtns"),
- ("wl_10.txt", "aoeuhtnsid"),
- ("wl_14.txt", "aoeuhtnsidpgkm"),
- ("wl_18.txt", "aoeuhtnsidpgkmyfxb"),
- ("wl_22.txt", "aoeuhtnsidpgkmyfxbcwjq"),
- ("wl_26.txt", "aoeuhtnsidpgkmyfxbcwjqrlvz")]
- shuffle :: [a] -> IO [a]
- shuffle xs = do
- ar <- newArray' n xs
- forM [1..n] $ \i -> do
- j <- randomRIO (i,n)
- vi <- readArray ar i
- vj <- readArray ar j
- writeArray ar j vi
- return vj
- where
- n = length xs
- newArray' :: Int -> [a] -> IO (IOArray Int a)
- newArray' y ys = newListArray (1,y) ys
- consistsOf :: String -> String -> Bool
- consistsOf word letters = all (\letter -> letter `elem` letters ) word
- writeTo :: String -> [[String]] -> IO ()
- writeTo _ [] = return ()
- writeTo fn (x:xs) =
- mapM_ ( \word -> appendFile fn $ word ++ " " ) x
- >> appendFile fn "\n"
- >> writeTo fn xs
- createWordList :: (String, String) -> IO ()
- createWordList (filename, letters) =
- writeFile filename ""
- >> readFile "dictionary.txt"
- >>= shuffle
- . filter (\word -> word `consistsOf` letters )
- . lines
- >>= writeTo filename
- . chunksOf 10
- prettyOption :: (Int, String) -> String
- prettyOption (n, w) = (show n) ++ ". " ++ w
- printOptions :: IO ()
- printOptions =
- mapM_ (putStrLn . prettyOption) printableLists
- where
- printableLists = zip ([1..] :: [Int]) $ map snd lists
- createList :: Int -> IO Bool
- createList n
- | 1 <= n && n <= 6 =
- putStrLn ("\nCreating: " ++ (fst (lists !! (n-1))))
- >> createWordList (lists !! (n-1) )
- >> return True
- | otherwise =
- putStrLn "\n***That is not a valid option***\n"
- >> return False
- getInt :: IO Int
- getInt =
- getLine
- >>= return
- . (fromMaybe 0 :: Maybe Int -> Int)
- . (readMaybe :: String -> Maybe Int)
- main :: IO ()
- main =
- printOptions
- >> putStr "\nChoose a list to create> "
- >> getInt
- >>= createList
- >>= \case
- True -> putStrLn "Done"
- False -> main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement