[Haskell] evaluate function as a string

Sebastian Sylvan sylvan at dtek.chalmers.se
Fri Apr 23 04:32:54 EDT 2004



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


More information about the Haskell mailing list