From ff5197de5c79ac94f6abf0983bce1838c3aa2ab5 Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Sun, 15 Sep 2024 17:06:40 +0200 Subject: [PATCH 1/3] fix pointing number parsing --- src/Lib.hs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Lib.hs b/src/Lib.hs index 172cced..e244e22 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -64,13 +64,14 @@ constantInteger = try (do constantRational :: Parser Rational constantRational = do - natural <- m_natural - _ <- char '.' - decimal <- m_natural + natural <- m_natural + _ <- char '.' + decimal_digits <- many digit + let decimal = read decimal_digits :: Integer let natural_length = length . show $ natural - let decimal_length = length . show $ decimal + let decimal_length = length decimal_digits let numerator = natural * (10 ^ decimal_length) + decimal - let denominator = 10 ^ (decimal_length + natural_length - 2) + let denominator = 10 ^ (decimal_length + natural_length - 1) return (numerator % denominator) {- From b4bbd298a026594e8f7a792ae6072b7e5207a04d Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Sun, 15 Sep 2024 17:58:43 +0200 Subject: [PATCH 2/3] replacing Variables with constant values --- hc.cabal | 3 +++ package.yaml | 1 + src/Lib.hs | 11 +++++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/hc.cabal b/hc.cabal index 40d475c..ed1564b 100644 --- a/hc.cabal +++ b/hc.cabal @@ -35,6 +35,7 @@ library ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints build-depends: base >=4.7 && <5 + , containers , parsec default-language: Haskell2010 @@ -49,6 +50,7 @@ executable hc-exe ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N build-depends: base >=4.7 && <5 + , containers , hc , parsec default-language: Haskell2010 @@ -65,6 +67,7 @@ test-suite hc-test ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N build-depends: base >=4.7 && <5 + , containers , hc , parsec default-language: Haskell2010 diff --git a/package.yaml b/package.yaml index 2287f3b..6dcae46 100644 --- a/package.yaml +++ b/package.yaml @@ -22,6 +22,7 @@ description: Please see the README on GitHub at = 4.7 && < 5 - parsec +- containers ghc-options: - -Wall diff --git a/src/Lib.hs b/src/Lib.hs index e244e22..d83b50d 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -1,9 +1,11 @@ module Lib - ( exprparser, evaluate + ( exprparser, evaluate, replaceVars ) where import Control.Applicative((<*)) import Data.Ratio +import Data.Map (Map) +import qualified Data.Map as Map import Text.Parsec import Text.Parsec.Char @@ -26,7 +28,7 @@ def = emptyDef{ commentStart = "" , opStart = oneOf "+-/*^" , opLetter = oneOf "+-/*^" , reservedOpNames = ["+", "-", "/", "*", "^"] - , reservedNames = ["pi", "e"] + , reservedNames = [] } TokenParser{ parens = m_parens @@ -89,6 +91,11 @@ term = m_parens exprparser <|> fmap Constant constantInteger <|> fmap Constant constantRational +replaceVars :: Expr -> Map.Map String Rational -> Expr +replaceVars (Variable name) vs = Constant . maybe (0 % 1) id $ Map.lookup name vs +replaceVars (Binary op a b) vs = Binary op (replaceVars a vs) (replaceVars b vs) +replaceVars (Constant c) vs = Constant c + evaluate :: Expr -> Rational evaluate (Constant c) = c evaluate (Binary Plus a b) = evaluate a + evaluate b From e38576c1d8393881a70020e5114e282dcd8ebda1 Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Sun, 15 Sep 2024 17:58:53 +0200 Subject: [PATCH 3/3] Output with precision 5 --- app/Main.hs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 9e821d7..054050c 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -2,16 +2,31 @@ module Main (main) where import Text.Parsec -import Lib (exprparser, evaluate) +import Lib (exprparser, evaluate, replaceVars) + +import Data.Map (Map) +import qualified Data.Map as Map +import Data.Ratio import System.IO main :: IO () main = ioLoop -useResult (Right e) = show . evaluate $ e +precision = 5 :: Int + +showRatio :: Int -> Rational -> String +showRatio p r = (if (r < 0) then "-" else "") ++ prepoint_digits ++ "." ++ postpoint_digits + where + prepoint_digits = init . show . round $ (r * 10) + postpoint_digits = (take p) . (drop (length prepoint_digits)) . show . round $ (r * 10^p) + +useResult (Right e) = (showRatio precision) . evaluate $ replaceVars e vars useResult (Left e) = show e +vars :: Map String Rational +vars = Map.fromList [("pi", 245850922 % 78256779), ("e", 271801 % 99990)] + ioLoop :: IO () ioLoop = do done <- isEOF if done