-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic.hs
47 lines (39 loc) · 1.13 KB
/
arithmetic.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Main (
main
) where
import System.Random --cabal install random
import System.Exit
getPair :: [Int] -> [(Int, Int)]
getPair [] = []
getPair [x] = []
getPair (x:y:s) = (x, y) : (getPair s)
--a Haskell FP recursive style to do for-loop equivalent in imperative language
ques :: [(Int, Int)] -> Int -> IO (Int)
ques [] sc = do
putStrLn ""
return sc
ques ((x,y):xs) sc = do
putStrLn ((show x) ++ " + " ++ (show y) ++ " = ?")
let expect = x+y
actual <- readLn :: IO Int
if actual == expect
then do
putStrLn "Right!"
ques xs (sc+1)
else do
putStrLn "Wrong!"
ques xs sc
--main :: IO ExitCode
main = do
putStrLn "How many questions do you want (3-20)?"
totalnum <- readLn :: IO Int
if totalnum < 3
then do
putStrLn ("Please input a digit >= 3, exit...")
exitFailure
else putStrLn "Start..."
g <- getStdGen
let ss = take (totalnum*2) (randomRs (0, 9) g :: [Int])
let pairs = getPair ss
score <- ques pairs 0
putStrLn ("Full score: " ++ (show totalnum) ++ ". Your score: " ++ (show score))