fix: negative numbers were causing trouble

This commit is contained in:
Mahdi Dibaiee 2016-11-04 17:41:43 +03:30
parent 1b61ed32fb
commit 8b8c8b9d65
2 changed files with 7 additions and 3 deletions

View File

@ -1,5 +1,5 @@
name: mathexpr name: mathexpr
version: 0.1.0.0 version: 0.2.0.0
synopsis: Parse and evaluate math expressions with variables and functions synopsis: Parse and evaluate math expressions with variables and functions
description: A simple tool to evaluate math expressions as strings with support for custom functions and operators description: A simple tool to evaluate math expressions as strings with support for custom functions and operators
homepage: https://github.com/mdibaiee/mathexpr homepage: https://github.com/mdibaiee/mathexpr

View File

@ -1,3 +1,4 @@
{-# Language ScopedTypeVariables #-}
module Data.MathExpr module Data.MathExpr
( evaluate ( evaluate
, Settings (..) , Settings (..)
@ -6,7 +7,6 @@ module Data.MathExpr
) where ) where
import Data.Default.Class import Data.Default.Class
import Data.Maybe (isJust, fromJust) import Data.Maybe (isJust, fromJust)
import Debug.Trace
import Data.List (find) import Data.List (find)
data Settings = Settings { operators :: [(Char, Int, Double -> Double -> Double)] data Settings = Settings { operators :: [(Char, Int, Double -> Double -> Double)]
@ -78,9 +78,13 @@ module Data.MathExpr
fns = functions settings fns = functions settings
helper :: [String] -> [String] -> Double helper :: [String] -> [String] -> Double
-- negative numbers come in the form ["num", "-"]
helper [] [o] = read o helper [] [o] = read o
helper (c:cs) os helper (c:cs) os
| isOperator c = | c == "-" && length os < 2 =
let result :: Double = negate . read . head $ cs
in helper (tail cs) $ (show result) : os
| isOperator c && length os >= 2 =
let result = (operatorFunction c) (read . head . tail $ os) (read . head $ os) let result = (operatorFunction c) (read . head . tail $ os) (read . head $ os)
in helper cs $ (show result) : drop 2 os in helper cs $ (show result) : drop 2 os
| isFunction c = | isFunction c =