aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--src/equation.hs20
-rw-r--r--src/main.hs1
-rw-r--r--src/parser.hs34
4 files changed, 37 insertions, 20 deletions
diff --git a/README.md b/README.md
index 88e47fb..315a479 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,3 @@
# computorv1
-Computorv1 project of school 42
+computorv1 project of school 42
diff --git a/src/equation.hs b/src/equation.hs
index 412428e..9053fa0 100644
--- a/src/equation.hs
+++ b/src/equation.hs
@@ -1,4 +1,11 @@
-module Equation where
+module Equation
+( Equation (..)
+, Polynomial
+, Term (..)
+, degree
+, reduce
+, solve
+) where
import Data.List
@@ -16,15 +23,16 @@ instance Ord Term where
instance Show Term where
show (Term 0 e) = ""
show (Term c 0) = show (round c)
- show (Term c e)
- | c < 0 = " - " ++ showInside (-c)
- | c > 0 = " + " ++ showInside c
- where showInside co = show (round co) ++ " * X^" ++ show e
+ show (Term c e) = show (round c) ++ " * X^" ++ show e
instance Show Equation where
show (Equation l r) = showPolynomial l ++ " = " ++ showPolynomial r
where showPolynomial [] = "0"
- showPolynomial p = concatMap show (filter (\(Term c _) -> c /= 0) p)
+ showPolynomial p = dropWhile (`elem` " +") $ foldl f "" (map show p)
+ where f s (c:cs)
+ | c == '-' = s ++ " - " ++ cs
+ | otherwise = s ++ " + " ++ (c:cs)
+
equationMap :: (Polynomial -> Polynomial) -> Equation -> Equation
equationMap f (Equation l r) = Equation (f l) (f r)
diff --git a/src/main.hs b/src/main.hs
index f466b68..20815f4 100644
--- a/src/main.hs
+++ b/src/main.hs
@@ -4,6 +4,7 @@ import Data.List
import Parser
import Equation
+
main :: IO ()
main = do
args <- getArgs
diff --git a/src/parser.hs b/src/parser.hs
index 49d23d7..6c2b8af 100644
--- a/src/parser.hs
+++ b/src/parser.hs
@@ -1,4 +1,7 @@
-module Parser where
+module Parser
+( parse
+, equationP
+) where
import Control.Applicative
import Control.Monad
@@ -43,40 +46,45 @@ charP x = Parser p
p (c:cs) = if c == x then Just (c, cs)
else Nothing
-satisfy :: (Char -> Bool) -> Parser String
-satisfy f = Parser (\s -> Just $ span f s)
+satisfy :: (Char -> Bool) -> Parser Char
+satisfy f = Parser p
+ where p [] = Nothing
+ p (c:cs) = if f c then Just (c, cs)
+ else Nothing
digitsP :: Parser String
-digitsP = satisfy isDigit
+digitsP = some (satisfy isDigit) -- at least one digit to avoid read exception
spacesP :: Parser String
-spacesP = satisfy isSpace
+spacesP = many (satisfy isSpace)
sepBy :: Parser a -> Parser b -> Parser [a]
sepBy x sep = (:) <$> x <*> many (sep *> x)
-surround :: Parser a -> Parser b -> Parser b
-surround surrounding x = surrounding *> x <* surrounding
-
--- Equation parsers
--- 1 * X^0 + 2 * X^1 + 1 * 3 * X^2 = 0
intP :: Parser Int
intP = read <$> numStr
- where numStr = ((:) <$> charP '-' <*> digitsP) <|> digitsP
+ where numStr = ((:) <$> charP '-' <*> (spacesP *> digitsP))
+ <|> (charP '+' *> spacesP *> digitsP)
+ <|> digitsP
+
+naturalP :: Parser Int
+naturalP = read <$> digitsP
+
+-- Equation parsers
termP :: Parser Term
termP = notConstantP <|> constantP
where constantP = (\c -> Term (fromIntegral c) 0) <$> intP
notConstantP = (\coef exp -> Term (fromIntegral coef) exp)
- <$> intP <*> (between *> intP)
+ <$> intP <*> (between *> naturalP)
where between = spacesP *> charP '*' *>
spacesP *> charP 'X' *>
spacesP *> charP '^' *>
spacesP
polynomialP :: Parser Polynomial
-polynomialP = sepBy termP (spacesP *> charP '+' *> spacesP)
+polynomialP = sepBy termP spacesP
equationP :: Parser Equation
equationP = (\l r -> Equation l r)