aboutsummaryrefslogtreecommitdiff
path: root/src/equation.hs
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-10 12:35:27 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-10 12:35:27 +0100
commite5ba91f99722d047f90f94a710d77344db36e110 (patch)
tree4c9b2c0e474eb1279d23a88623f0ee8b53391f50 /src/equation.hs
parent101739991c2ca919e3bb150b6df0041b27cf75a1 (diff)
downloadcomputorv1-e5ba91f99722d047f90f94a710d77344db36e110.tar.gz
computorv1-e5ba91f99722d047f90f94a710d77344db36e110.tar.bz2
computorv1-e5ba91f99722d047f90f94a710d77344db36e110.zip
Edge case handling in parsing and equation solving
Diffstat (limited to 'src/equation.hs')
-rw-r--r--src/equation.hs14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/equation.hs b/src/equation.hs
index 9053fa0..040812f 100644
--- a/src/equation.hs
+++ b/src/equation.hs
@@ -5,6 +5,7 @@ module Equation
, degree
, reduce
, solve
+, filterNull
) where
import Data.List
@@ -26,19 +27,26 @@ instance Show Term where
show (Term c e) = show (round c) ++ " * X^" ++ show e
instance Show Equation where
- show (Equation l r) = showPolynomial l ++ " = " ++ showPolynomial r
+ show (Equation l r) = showPolynomial (filterNull l)
+ ++ " = "
+ ++ showPolynomial (filterNull r)
where showPolynomial [] = "0"
showPolynomial p = dropWhile (`elem` " +") $ foldl f "" (map show p)
- where f s (c:cs)
+ where f s "" = s
+ f s (c:cs)
| c == '-' = s ++ " - " ++ cs
| otherwise = s ++ " + " ++ (c:cs)
+filterNull :: Polynomial -> Polynomial
+filterNull = filter (\t -> coefficient t /= 0)
+
equationMap :: (Polynomial -> Polynomial) -> Equation -> Equation
equationMap f (Equation l r) = Equation (f l) (f r)
degree :: Polynomial -> Int
-degree p = Equation.exponent (maximum p)
+degree [] = 0
+degree p = Equation.exponent (maximum p)
reduce :: Equation -> Equation
reduce equ = Equation (merge (left stdForm) (right stdForm)) []