Hello! I am new to Haskell. I want to develop an application in which the user enters a mathematical equation, presumably as a string (any arbitrary equation, using any of the standard mathematical operators : +,*,^, etc, and any of the standard functions: sin, exp, etc.). I need to take that string, convert it to a real function, and evaluate it. Thank you. ______________________________________________________________________ Correo Yahoo! - 6MB, más protección contra el spam ¡Gratis! http://correo.yahoo.es
On Thu, 2004-04-22 at 13:56, Ay Joselito wrote:
Hello! I am new to Haskell. I want to develop an application in which the user enters a mathematical equation, presumably as a string (any arbitrary equation, using any of the standard mathematical operators : +,*,^, etc, and any of the standard functions: sin, exp, etc.). I need to take that string, convert it to a real function, and evaluate it.
The standard solution would be to define a data structure to represent your expressions, then write a parser that converts a string into one of these data structures. Finally you need an evaluation function that reduces the expression data structure to the final answer. You could represent your expression using a data type like this: Data Exp = Number Double | OpPlus Exp Exp | OpTimes Exp Exp | FnSin Exp ... etc .. If your formulae can contain variables, it will be slightly more complicated than the above. You might look at one of the parser combinator libraries to write your parser, or use the parser generator 'Happy' (which may require using 'Alex' the scanner generator). Your evaluation function will operate recursively over the structure of the expression. It will have a case for each expression constructor. It'll look something like: eval :: Exp -> Double eval (Number n) = ... eval (OpPlus a b) = ... ... etc ... Hope this points you in the right direction. Duncan
Ay Joselito wrote:
I want to develop an application in which the user enters a mathematical equation, presumably as a string (any arbitrary equation, using any of the standard mathematical operators : +,*,^, etc, and any of the standard functions: sin, exp, etc.). I need to take that string, convert it to a real function, and evaluate it.
First you need to parse the expression into a Haskell data type. For this I would recommend Parsec (see Haskell.org). The Haskell data type would be something like data Expr = AtomD Double | AtomI Integer | Add Expr Expr | Mul Expr Expr | Sub Expr Expr | .... Then when you've parsed the expression you need to evaluate it. This should be fairly straightforward (pattern match against the constructors and perform the necessary operations recursively). Good luck. -- Clothes make the man. Naked people have little or no influence on society. - Mark Twain
On Fri, Apr 23, 2004 at 03:32:54AM +0200, Sebastian Sylvan wrote:
First you need to parse the expression into a Haskell data type. For this I would recommend Parsec (see Haskell.org). The Haskell data type would be something like
data Expr = AtomD Double | AtomI Integer | Add Expr Expr | Mul Expr Expr | Sub Expr Expr | ....
There is no need to introduce a datatype - for one parameter functions you can parse text directly to a (Double -> Double) function. Of course if you want to do more things with your functions than just evaluation (like printing, symbolic differentiation), introducing a datatype is a good idea. Best regards, Tom -- .signature: Too many levels of symbolic links
Tomasz Zielonka wrote:
On Fri, Apr 23, 2004 at 03:32:54AM +0200, Sebastian Sylvan wrote:
First you need to parse the expression into a Haskell data type. For this I would recommend Parsec (see Haskell.org). The Haskell data type would be something like
data Expr = AtomD Double | AtomI Integer | Add Expr Expr | Mul Expr Expr | Sub Expr Expr | ....
There is no need to introduce a datatype - for one parameter functions you can parse text directly to a (Double -> Double) function. Of course if you want to do more things with your functions than just evaluation (like printing, symbolic differentiation), introducing a datatype is a good idea.
The original poster used the term "equation" which to me indicated that he might have things like unknown variables in stuff. In other words: He would have to do some form of manipulation of the expression in order to get the result. /S -- Clothes make the man. Naked people have little or no influence on society. - Mark Twain
participants (4)
-
Ay Joselito -
Duncan Coutts -
Sebastian Sylvan -
Tomasz Zielonka