aboutsummaryrefslogtreecommitdiff
path: root/src/equation.hs
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-09 23:17:09 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-09 23:17:09 +0100
commit101739991c2ca919e3bb150b6df0041b27cf75a1 (patch)
tree4ca7427dcbcbb690bbf7037e4ab8065df376d648 /src/equation.hs
parent0b9af447ffa279f07bdcbc8201a6ad8d731ea95b (diff)
downloadcomputorv1-101739991c2ca919e3bb150b6df0041b27cf75a1.tar.gz
computorv1-101739991c2ca919e3bb150b6df0041b27cf75a1.tar.bz2
computorv1-101739991c2ca919e3bb150b6df0041b27cf75a1.zip
No more exception on digit parsing, coerrant equation display
Diffstat (limited to 'src/equation.hs')
-rw-r--r--src/equation.hs20
1 files changed, 14 insertions, 6 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)