Advertisement
Guest User

Dvorak Word Lists

a guest
Jun 1st, 2018
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #!/bin/env stack
  2. {-
  3. stack
  4. script
  5. --resolver lts-10.3
  6. --ghc-build tinfo6
  7. -}
  8.  
  9. {-# LANGUAGE LambdaCase #-}
  10.  
  11. module Lib where
  12.  
  13. import System.Random
  14. import Data.Array.IO
  15. import Control.Monad
  16. import Data.List.Split
  17. import Text.Read (readMaybe)
  18. import Data.Maybe
  19.  
  20. lists :: [(String, String)]
  21. lists = [("wl_8.txt", "aoeuhtns"),
  22. ("wl_10.txt", "aoeuhtnsid"),
  23. ("wl_14.txt", "aoeuhtnsidpgkm"),
  24. ("wl_18.txt", "aoeuhtnsidpgkmyfxb"),
  25. ("wl_22.txt", "aoeuhtnsidpgkmyfxbcwjq"),
  26. ("wl_26.txt", "aoeuhtnsidpgkmyfxbcwjqrlvz")]
  27.  
  28. shuffle :: [a] -> IO [a]
  29. shuffle xs = do
  30. ar <- newArray' n xs
  31. forM [1..n] $ \i -> do
  32. j <- randomRIO (i,n)
  33. vi <- readArray ar i
  34. vj <- readArray ar j
  35. writeArray ar j vi
  36. return vj
  37. where
  38. n = length xs
  39. newArray' :: Int -> [a] -> IO (IOArray Int a)
  40. newArray' y ys = newListArray (1,y) ys
  41.  
  42. consistsOf :: String -> String -> Bool
  43. consistsOf word letters = all (\letter -> letter `elem` letters ) word
  44.  
  45. writeTo :: String -> [[String]] -> IO ()
  46. writeTo _ [] = return ()
  47. writeTo fn (x:xs) =
  48. mapM_ ( \word -> appendFile fn $ word ++ " " ) x
  49. >> appendFile fn "\n"
  50. >> writeTo fn xs
  51.  
  52. createWordList :: (String, String) -> IO ()
  53. createWordList (filename, letters) =
  54. writeFile filename ""
  55. >> readFile "dictionary.txt"
  56. >>= shuffle
  57. . filter (\word -> word `consistsOf` letters )
  58. . lines
  59. >>= writeTo filename
  60. . chunksOf 10
  61.  
  62. prettyOption :: (Int, String) -> String
  63. prettyOption (n, w) = (show n) ++ ". " ++ w
  64.  
  65. printOptions :: IO ()
  66. printOptions =
  67. mapM_ (putStrLn . prettyOption) printableLists
  68. where
  69. printableLists = zip ([1..] :: [Int]) $ map snd lists
  70.  
  71. createList :: Int -> IO Bool
  72. createList n
  73. | 1 <= n && n <= 6 =
  74. putStrLn ("\nCreating: " ++ (fst (lists !! (n-1))))
  75. >> createWordList (lists !! (n-1) )
  76. >> return True
  77. | otherwise =
  78. putStrLn "\n***That is not a valid option***\n"
  79. >> return False
  80.  
  81. getInt :: IO Int
  82. getInt =
  83. getLine
  84. >>= return
  85. . (fromMaybe 0 :: Maybe Int -> Int)
  86. . (readMaybe :: String -> Maybe Int)
  87.  
  88. main :: IO ()
  89. main =
  90. printOptions
  91. >> putStr "\nChoose a list to create> "
  92. >> getInt
  93. >>= createList
  94. >>= \case
  95. True -> putStrLn "Done"
  96. False -> main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement