diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-03-09 23:17:09 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-03-09 23:17:09 +0100 |
| commit | 101739991c2ca919e3bb150b6df0041b27cf75a1 (patch) | |
| tree | 4ca7427dcbcbb690bbf7037e4ab8065df376d648 /src | |
| parent | 0b9af447ffa279f07bdcbc8201a6ad8d731ea95b (diff) | |
| download | computorv1-101739991c2ca919e3bb150b6df0041b27cf75a1.tar.gz computorv1-101739991c2ca919e3bb150b6df0041b27cf75a1.tar.bz2 computorv1-101739991c2ca919e3bb150b6df0041b27cf75a1.zip | |
No more exception on digit parsing, coerrant equation display
Diffstat (limited to 'src')
| -rw-r--r-- | src/equation.hs | 20 | ||||
| -rw-r--r-- | src/main.hs | 1 | ||||
| -rw-r--r-- | src/parser.hs | 34 |
3 files changed, 36 insertions, 19 deletions
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) |
