ParserFunction 0.0.5 → 0.0.6
raw patch · 17 files changed
+12/−10 lines, 17 files
Files
- .DS_Store binary
- ._.DS_Store binary
- ._LICENSE binary
- ._ParserFunction.cabal binary
- ._Setup.hs binary
- ._Text binary
- ParserFunction.cabal +8/−5
- Text/.DS_Store binary
- Text/._.DS_Store binary
- Text/._ParserCombinators binary
- Text/ParserCombinators/.DS_Store binary
- Text/ParserCombinators/._.DS_Store binary
- Text/ParserCombinators/._Parsec binary
- Text/ParserCombinators/Parsec/.DS_Store binary
- Text/ParserCombinators/Parsec/._.DS_Store binary
- Text/ParserCombinators/Parsec/._ParserFunction.hs binary
- Text/ParserCombinators/Parsec/ParserFunction.hs +4/−5
− .DS_Store
binary file changed (6148 → absent bytes)
− ._.DS_Store
binary file changed (197 → absent bytes)
− ._LICENSE
binary file changed (244 → absent bytes)
− ._ParserFunction.cabal
binary file changed (167 → absent bytes)
− ._Setup.hs
binary file changed (197 → absent bytes)
− ._Text
binary file changed (197 → absent bytes)
ParserFunction.cabal view
@@ -1,5 +1,5 @@ name: ParserFunction-version: 0.0.5+version: 0.0.6 cabal-version: >= 1.6 license: BSD3 license-file: LICENSE@@ -9,10 +9,13 @@ synopsis: Utilities for parsing and evaluating mathematical expressions. description: ParserFunction provides utilities for parsing and evaluating mathematical expressions.- The central parsing function in this package is @stringToExpr@, which parses an expression- (as a string) and returns an expression tree of type Expr (or nothing if the string is malformed).- Expressions can be evaluated using the function @evaluateExpression@. Examples of these- functions can be found by viewing the source code for this package.+ The central parsing function in this package is @stringToExpr@, which parses a string-expression+ (e.g. \"3*x+2\") and returns a Maybe expression tree of type Expr (e.g. Just (Add (Mul (Num 3.0) (Var \'x\')) (Num 2.0))).+ This type is suitable for performing symbolic logic. Expressions can then be evaluated using the function @evaluate@+ (e.g. @evaluate@ (fromAscList [(\"x\",2)]) (Add (Mul (Num 3.0) (Var \'x\'))) (Num 2.0) would give 8.0).+ If you wish to evaluate a string-expression without any intermediate symbolic logic operations, simply use the function+ @evaluateExpression@ (e.g. @evaluateExpression@ \"3*x+2\" [(\'x\',4)] gives 14.0). More examples of these functions can be found+ by viewing the source code for this package. build-type: Simple
− Text/.DS_Store
binary file changed (6148 → absent bytes)
− Text/._.DS_Store
binary file changed (197 → absent bytes)
− Text/._ParserCombinators
binary file changed (197 → absent bytes)
− Text/ParserCombinators/.DS_Store
binary file changed (6148 → absent bytes)
− Text/ParserCombinators/._.DS_Store
binary file changed (197 → absent bytes)
− Text/ParserCombinators/._Parsec
binary file changed (197 → absent bytes)
− Text/ParserCombinators/Parsec/.DS_Store
binary file changed (6148 → absent bytes)
− Text/ParserCombinators/Parsec/._.DS_Store
binary file changed (197 → absent bytes)
− Text/ParserCombinators/Parsec/._ParserFunction.hs
binary file changed (171 → absent bytes)
Text/ParserCombinators/Parsec/ParserFunction.hs view
@@ -31,7 +31,7 @@ import Data.List (isInfixOf) import Data.Char (toLower) --- The Expr data type provides a basis for ordering mathematical operations.+-- |The Expr data type provides a basis for ordering mathematical operations. data Expr = Num Double | Var Char | Sub Expr Expr | Div Expr Expr | Pow Expr Expr | Log Expr | Abs Expr | Sqrt Expr | Cbrt Expr@@ -45,15 +45,14 @@ | Sec Expr | Csc Expr | Cot Expr | Mul Expr Expr | Add Expr Expr | Exp Expr deriving (Show, Eq, Ord) ---- evaluateExpression evaluates a mathematical expression s using the variable map m. +-- |@evaluateExpression@ evaluates a mathematical expression s using the variable map m. evaluateExpression :: String -> [(Char,Double)] -> Double evaluateExpression s m = evaluate (M.fromAscList $ caseMap m) (fromMaybe failing $ stringToExpr s) where caseMap x = fmap (\ (a, b) -> ([toLower a], b)) x failing = error "Parser error in expression" ---- stringToExpr parses an expression and returns an expression tree of type Expr, ---- (or nothing if the string is malformed).+-- |@stringToExpr@ parses an expression and returns an expression tree of type Expr. stringToExpr :: String -> Maybe Expr stringToExpr xs = if any (==True) (symbols failingSymbols xs) then Nothing@@ -113,7 +112,7 @@ fe = toInteger . fromEnum ch2num = (subtract $ fe '0') . fe ---- evaluate takes a map and expression to produce a numerical value.+-- |@evaluate@ takes a map and expression tree to produce a numerical value. evaluate :: M.Map String Double -> Expr -> Double evaluate m expr = case expr of