a33ebf4639
Add new files
36 lines
807 B
Haskell
36 lines
807 B
Haskell
module Diceware where
|
|
|
|
import qualified Data.Map as Map
|
|
import List
|
|
|
|
-- Diceware dictionary type
|
|
type Diceware = Map.Map Int String
|
|
|
|
-- Parse a line into a tuple
|
|
parseLine :: String -> (Int, String)
|
|
parseLine x =
|
|
(read a :: Int, b)
|
|
where
|
|
x' = split x ' '
|
|
(a, b) = (head x', last x')
|
|
|
|
-- Parse file content to a Diceware
|
|
parseDiceware :: String -> Diceware
|
|
parseDiceware x = (Map.fromList . map parseLine . lines) x
|
|
|
|
-- Lookup word with dice index
|
|
readDiceware :: Diceware -> Int -> String
|
|
readDiceware d n =
|
|
show n ++ " -> " ++
|
|
case Map.lookup n d of
|
|
Just x -> x
|
|
Nothing -> "Does not exist"
|
|
|
|
-- Lookup word with linear index
|
|
readDiceware' :: Diceware -> Int -> String
|
|
readDiceware' d n = (snd . (!!n) . Map.toList) d
|
|
|
|
-- Size of Diceware (should be 2^5)
|
|
size :: Diceware -> Int
|
|
size = Map.size
|