Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Parse and evaluate mathematical expressions.
- data Expr a
- = Num a
- | Var String
- | Neg (Expr a)
- | Add (Expr a) (Expr a)
- | Sub (Expr a) (Expr a)
- | Mul (Expr a) (Expr a)
- | Div (Expr a) (Expr a)
- | Pow (Expr a) (Expr a)
- | Sqrt (Expr a)
- | Exp (Expr a)
- | Log (Expr a)
- | Abs (Expr a)
- | Sin (Expr a)
- | Cos (Expr a)
- | Tan (Expr a)
- | Sec (Expr a)
- | Csc (Expr a)
- | Cot (Expr a)
- | Sinh (Expr a)
- | Cosh (Expr a)
- | Tanh (Expr a)
- | Sech (Expr a)
- | Csch (Expr a)
- | Coth (Expr a)
- | ArcSin (Expr a)
- | ArcCos (Expr a)
- | ArcTan (Expr a)
- | ArcSec (Expr a)
- | ArcCsc (Expr a)
- | ArcCot (Expr a)
- | ArcSinh (Expr a)
- | ArcCosh (Expr a)
- | ArcTanh (Expr a)
- | ArcSech (Expr a)
- | ArcCsch (Expr a)
- | ArcCoth (Expr a)
- parse :: Floating a => String -> Either ParseError (Expr a)
- data ParseError :: *
- evaluate :: Floating a => Map String a -> Maybe (Expr a) -> Maybe a
Expressions
Mathematical expressions.
Num a | |
Var String | |
Neg (Expr a) | |
Add (Expr a) (Expr a) | |
Sub (Expr a) (Expr a) | |
Mul (Expr a) (Expr a) | |
Div (Expr a) (Expr a) | |
Pow (Expr a) (Expr a) | |
Sqrt (Expr a) | |
Exp (Expr a) | |
Log (Expr a) | |
Abs (Expr a) | |
Sin (Expr a) | |
Cos (Expr a) | |
Tan (Expr a) | |
Sec (Expr a) | |
Csc (Expr a) | |
Cot (Expr a) | |
Sinh (Expr a) | |
Cosh (Expr a) | |
Tanh (Expr a) | |
Sech (Expr a) | |
Csch (Expr a) | |
Coth (Expr a) | |
ArcSin (Expr a) | |
ArcCos (Expr a) | |
ArcTan (Expr a) | |
ArcSec (Expr a) | |
ArcCsc (Expr a) | |
ArcCot (Expr a) | |
ArcSinh (Expr a) | |
ArcCosh (Expr a) | |
ArcTanh (Expr a) | |
ArcSech (Expr a) | |
ArcCsch (Expr a) | |
ArcCoth (Expr a) |
Parsing
parse :: Floating a => String -> Either ParseError (Expr a) Source
Parse a mathematical expression.
>>> parse "exp(-pi*i)+1" Right (Add (Exp (Mul (Neg (Var "pi")) (Var "i"))) (Num 1.0))
data ParseError :: *
Evaluation
:: Floating a | |
=> Map String a | Variable definitions. |
-> Maybe (Expr a) | Mathematical expression. |
-> Maybe a |
Evaluate a mathematical expression using the supplied variable definitions.
>>> :m + Data.Complex Data.Map >>> let def = fromList [("pi", pi), ("i", 0 :+ 1)] >>> evaluate def . Just $ Add (Exp (Mul (Neg (Var "pi")) (Var "i"))) (Num 1.0) Just (0.0 :+ (-1.2246467991473532e-16))