-
Notifications
You must be signed in to change notification settings - Fork 0
/
General.hs
67 lines (57 loc) · 1.31 KB
/
General.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
58
59
60
61
62
63
64
65
66
67
module General where
import Text.Parsec
import Text.Parsec.String
import Tokens
data SolutionType
= NoRealSolutions
| OneSolution
| TwoSolutions
deriving (Eq)
instance Show SolutionType where
show NoRealSolutions = "no real solutions"
show OneSolution = "one solution"
show TwoSolutions = "two solutions"
typeOfSolutions :: Double -> SolutionType
typeOfSolutions disc
| disc < 0 = NoRealSolutions
| disc == 0 = OneSolution
| disc > 0 = TwoSolutions
| otherwise = NoRealSolutions
extractNumAndPow :: Tokens -> (Double, Double)
extractNumAndPow
( Power
( Coefficient
( Number a,
_
),
Number b
)
) = (a, b)
extractNumAndPow _ = error "No equation provided"
whitespace :: Parser Tokens
whitespace = do
many1 space
return Space
op :: Parser Tokens
op = do
sign <- oneOf "+-"
return $
Operator
( if sign == '-'
then Minus
else Plus
)
opInFrontOfTerm :: Parser Tokens
opInFrontOfTerm = do
o <- try op <|> return (Operator Plus)
_ <- try whitespace <|> return Space
return o
number :: Parser Tokens
number = do
o <- opInFrontOfTerm
n <- many1 digit
if o == Operator Minus
then return $ Number (read n * (-1))
else return $ Number (read n)
power :: Parser Tokens
power = char '^' >> number