Compare commits

..

No commits in common. "e38576c1d8393881a70020e5114e282dcd8ebda1" and "107f887147890d679b945ef52e4cb80365e31ed5" have entirely different histories.

4 changed files with 9 additions and 36 deletions

View file

@ -2,31 +2,16 @@ module Main (main) where
import Text.Parsec
import Lib (exprparser, evaluate, replaceVars)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Ratio
import Lib (exprparser, evaluate)
import System.IO
main :: IO ()
main = ioLoop
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 (Right e) = show . evaluate $ e
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

View file

@ -35,7 +35,6 @@ 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
@ -50,7 +49,6 @@ 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
@ -67,7 +65,6 @@ 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

View file

@ -22,7 +22,6 @@ description: Please see the README on GitHub at <https://github.com/gith
dependencies:
- base >= 4.7 && < 5
- parsec
- containers
ghc-options:
- -Wall

View file

@ -1,11 +1,9 @@
module Lib
( exprparser, evaluate, replaceVars
( exprparser, evaluate
) where
import Control.Applicative((<*))
import Data.Ratio
import Data.Map (Map)
import qualified Data.Map as Map
import Text.Parsec
import Text.Parsec.Char
@ -28,7 +26,7 @@ def = emptyDef{ commentStart = ""
, opStart = oneOf "+-/*^"
, opLetter = oneOf "+-/*^"
, reservedOpNames = ["+", "-", "/", "*", "^"]
, reservedNames = []
, reservedNames = ["pi", "e"]
}
TokenParser{ parens = m_parens
@ -66,14 +64,13 @@ constantInteger = try (do
constantRational :: Parser Rational
constantRational = do
natural <- m_natural
_ <- char '.'
decimal_digits <- many digit
let decimal = read decimal_digits :: Integer
natural <- m_natural
_ <- char '.'
decimal <- m_natural
let natural_length = length . show $ natural
let decimal_length = length decimal_digits
let decimal_length = length . show $ decimal
let numerator = natural * (10 ^ decimal_length) + decimal
let denominator = 10 ^ (decimal_length + natural_length - 1)
let denominator = 10 ^ (decimal_length + natural_length - 2)
return (numerator % denominator)
{-
@ -91,11 +88,6 @@ 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