-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEquation.hs
57 lines (43 loc) · 1.63 KB
/
Equation.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
48
49
50
51
52
53
54
55
56
57
module Equation where
import Operator
import Regimen
import EFA
-- Equation functions
data Equation = Equation (Operator, (Integer, Integer))
toEquation :: (Operator, (Integer, Integer)) -> Equation
toEquation a = (Equation a)
instance Show Equation where
show e = printEquation e
printEquation :: Equation -> [Char]
printEquation (Equation (op, (a, b))) = (show a) ++ " " ++ (showOperator op) ++ " " ++ (show b)
eval :: Equation -> Integer
eval (Equation (op, (a,b))) = evalOperator op a b
evalMath :: Equation -> Integer -> Result
evalMath eq input = if input == answer
then (True, "Correct!")
else (False, ("Wrong... " ++ (show answer)))
where answer = eval eq
{- IO
I struggled with trying to seperate out the IO from the pure functions.
Once one thing down the line needs a bit of IO suddenly your whole stack
needs it and writing the functions becomes just that much harder. I believe
I landed on a nice middle ground here. A little IO, some pure functions, a
little IO... etc.
-}
etraining :: [Equation] -> IO [Bool]
etraining [] = return []
etraining (eq:eqs) = do
putStrLn $ printEquation eq
input <- readLn :: IO Integer
let (correct, outMsg) = evalMath eq input
putStrLn outMsg
remaining <- etraining eqs
return (correct:remaining)
genEquations :: Regimen -> IO [Equation]
genEquations (R {times = t, maxNum = m, maxEnum = me}) = do
xs <- randos t m
ys <- randos t m
opNums <- randos t me
let numbers = zip xs ys
let ops = map numToOperator opNums
return (map toEquation (zip ops numbers))