[Haskell-cafe] Evaluating arithmetic expressions at run time

Ben Lippmeier Ben.Lippmeier at anu.edu.au
Sat Jan 28 01:49:25 EST 2006


BTW: There isn't an easy way to magically derive opTable. We're relying 
on the fact that these simple functions all have the same type.

Ben Lippmeier wrote:
> 
> opTable
>  = [ ("+", (+))
>    , ("-", (-)) ... ]
> 
> myeval x y op
>  = let    Just fun = lookup op opTable
>    in   x `fun` y
> 


You could also do

myeval x y op
  = case op of
	"+"	-> x + y
	"-"	-> x - y
...

is probably the "smallest" way.

Ben.


More information about the Haskell-Cafe mailing list