replacing Variables with constant values
This commit is contained in:
parent
ff5197de5c
commit
b4bbd298a0
3 changed files with 13 additions and 2 deletions
3
hc.cabal
3
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
|
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
|
||||||
build-depends:
|
build-depends:
|
||||||
base >=4.7 && <5
|
base >=4.7 && <5
|
||||||
|
, containers
|
||||||
, parsec
|
, parsec
|
||||||
default-language: Haskell2010
|
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
|
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:
|
build-depends:
|
||||||
base >=4.7 && <5
|
base >=4.7 && <5
|
||||||
|
, containers
|
||||||
, hc
|
, hc
|
||||||
, parsec
|
, parsec
|
||||||
default-language: Haskell2010
|
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
|
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:
|
build-depends:
|
||||||
base >=4.7 && <5
|
base >=4.7 && <5
|
||||||
|
, containers
|
||||||
, hc
|
, hc
|
||||||
, parsec
|
, parsec
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
|
@ -22,6 +22,7 @@ description: Please see the README on GitHub at <https://github.com/gith
|
||||||
dependencies:
|
dependencies:
|
||||||
- base >= 4.7 && < 5
|
- base >= 4.7 && < 5
|
||||||
- parsec
|
- parsec
|
||||||
|
- containers
|
||||||
|
|
||||||
ghc-options:
|
ghc-options:
|
||||||
- -Wall
|
- -Wall
|
||||||
|
|
11
src/Lib.hs
11
src/Lib.hs
|
@ -1,9 +1,11 @@
|
||||||
module Lib
|
module Lib
|
||||||
( exprparser, evaluate
|
( exprparser, evaluate, replaceVars
|
||||||
) where
|
) where
|
||||||
import Control.Applicative((<*))
|
import Control.Applicative((<*))
|
||||||
|
|
||||||
import Data.Ratio
|
import Data.Ratio
|
||||||
|
import Data.Map (Map)
|
||||||
|
import qualified Data.Map as Map
|
||||||
|
|
||||||
import Text.Parsec
|
import Text.Parsec
|
||||||
import Text.Parsec.Char
|
import Text.Parsec.Char
|
||||||
|
@ -26,7 +28,7 @@ def = emptyDef{ commentStart = ""
|
||||||
, opStart = oneOf "+-/*^"
|
, opStart = oneOf "+-/*^"
|
||||||
, opLetter = oneOf "+-/*^"
|
, opLetter = oneOf "+-/*^"
|
||||||
, reservedOpNames = ["+", "-", "/", "*", "^"]
|
, reservedOpNames = ["+", "-", "/", "*", "^"]
|
||||||
, reservedNames = ["pi", "e"]
|
, reservedNames = []
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenParser{ parens = m_parens
|
TokenParser{ parens = m_parens
|
||||||
|
@ -89,6 +91,11 @@ term = m_parens exprparser
|
||||||
<|> fmap Constant constantInteger
|
<|> fmap Constant constantInteger
|
||||||
<|> fmap Constant constantRational
|
<|> 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 :: Expr -> Rational
|
||||||
evaluate (Constant c) = c
|
evaluate (Constant c) = c
|
||||||
evaluate (Binary Plus a b) = evaluate a + evaluate b
|
evaluate (Binary Plus a b) = evaluate a + evaluate b
|
||||||
|
|
Loading…
Reference in a new issue