From df5d3f05ead68a6cdecfeccecadce45fd4b574fd Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 10 Mar 2020 14:57:47 +0100 Subject: Solution commentary, parsing free from entrie (i.e X^1, 2*X) bonus --- src/main.hs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/main.hs') diff --git a/src/main.hs b/src/main.hs index a11f7fc..f0a83e5 100644 --- a/src/main.hs +++ b/src/main.hs @@ -23,9 +23,7 @@ tryMain = do l = filterNull $ left reduced putStrLn $ "Reduced From: " ++ show reduced putStrLn $ "Polynomial degree: " ++ (show $ degree l) - case l of [] -> putStrLn "Infinite solutions" - [_] -> putStrLn "No solution" - _ -> putSolutions l + putSolutions l checkArgs :: [String] -> IO () checkArgs args @@ -40,6 +38,13 @@ checkParsing input = case parse Parser.equationP input Just (_, s) -> fail "Couldnt parse equation yo" putSolutions :: Polynomial -> IO () -putSolutions p - | degree p > 2 = fail "The polynomial degree is strictly greater then 2, can't solve." - | otherwise = putStr $ intercalate "\n" (map show (solve p)) +putSolutions [] = putStrLn "Infinite solutions" -- 0 = 0 +putSolutions [_] = putStrLn "No solution" -- c = 0 +putSolutions p = case degree p of + 1 -> putStrLn $ "The solution is:\n" ++ show (head solutions) + 2 -> do case length solutions of 0 -> putStrLn "Discriminant is strictly negative, there is no solution." + 1 -> putStrLn "Discriminant is equal to 0, the solution is:" + 2 -> putStrLn "Discriminant is strictly positive, the two solutions are:" + putStr $ intercalate "\n" $ map show solutions + _ -> fail "The polynomial degree is strictly greater then 2, can't solve." + where solutions = sort $ solve p -- cgit