Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- should probably have used Data.Map for dictionary instead of list of pairs
- -- also, 100% of functions are IO actions!
- import System.IO
- import System.Random
- type Dict = [(String, String)]
- -- calls main loop with empty dictionary
- main :: IO ()
- main = mainloop []
- mainloop :: Dict -> IO ()
- mainloop dict =
- do printGreeting
- selection <- getLine
- performActivity dict selection
- -- this probably doesn't need to be its own function
- printGreeting :: IO ()
- printGreeting = putStrLn "e to enter words into dictionary\ns to start testing"
- -- procedure called by the main loop to start up either
- -- word entry or the test itself
- performActivity :: Dict -> String -> IO ()
- performActivity dict selection
- | selection == "e" = dictEntry dict >>= mainloop
- | selection == "s" = doGame dict >> mainloop dict
- | otherwise = putStrLn "Invalid entry" >> mainloop dict
- -- user prompt to enter things into dictionary
- -- to do: allow removal from dictionary
- -- allow read/write dictionary to/from file
- dictEntry :: Dict -> IO Dict
- dictEntry dict =
- do a <- getLine
- if (a == "EXIT") then return dict else
- do b <- getLine
- if (b == "EXIT") then return dict else
- let newdict = (:) (a, b) dict in (dictEntry newdict)
- -- the actual test component
- doGame :: Dict -> IO ()
- doGame dict
- | dict == [] = putStrLn "No entries in dictionary." >> mainloop dict
- | otherwise =
- do choice <- randomIndex
- let pair = dict !! choice
- response <- doQuestion pair
- if response == "EXIT"
- then return ()
- else if response == snd pair
- then doGame dict
- else putStrLn (snd pair) >> doGame dict
- where randomIndex = randomRIO (0, (length dict) - 1)
- -- prints word to translate and accepts user input
- doQuestion :: (String, String) -> IO String
- doQuestion pair =
- do putStrLn $ fst pair
- getLine
Add Comment
Please, Sign In to add comment