[Haskell-cafe] Evaluating arithmetic expressions at run time

Brian Hulley brianh at metamilk.com
Sat Jan 28 06:39:39 EST 2006


Andrew Savige wrote:
> --- Cale Gibbard wrote:
>> Apart from moving to a lookup Map or something, a simple reordering
>> of the arguments allows you to shorten things up a bit:
>> 
>> myeval :: String -> Int -> Int -> Int
>> myeval "+" = (+)
>> myeval "-" = (-)
>> myeval "*" = (*)
>> etc.
> 
> Thanks to all for the excellent suggestions. I'm liking the
> Haskell version of this function a lot more now. :-)
> 
> To help me learn Haskell, I'm converting a small Ruby
> program to Haskell. In Ruby this can be done in one line:
> 
> def myeval(x, y, op)
>   x.send op, y
> end

This could be done in Haskell by

myeval :: a->b->(a -> b -> c) -> c
myeval x y op = op x y

eg myeval (+) 1 2

Regards, Brian.


More information about the Haskell-Cafe mailing list