[Haskell-cafe] Evaluating arithmetic expressions at run time
Ben Lippmeier
Ben.Lippmeier at anu.edu.au
Sat Jan 28 01:39:27 EST 2006
Andrew Savige wrote:
> Haskell beginner using GHC.
Hello there!
How about,
opTable
= [ ("+", (+))
, ("-", (-)) ... ]
myeval x y op
= let Just fun = lookup op opTable
in x `fun` y
?
>
> I have a function to do some simple arithmetic at run time:
>
> myeval :: Int -> Int -> String -> Int
> myeval x y "+" = (+) x y
> myeval x y "-" = (-) x y
> myeval x y "*" = (*) x y
> -- ...
>
> While that works, I'm curious to know if it can be done more
> elegantly. I'm thinking of something like:
>
> myeval :: Int -> Int -> String -> Int
> myeval x y op = (read op) x y
>
> Thanks,
> /-\
>
>
>
>
> ____________________________________________________
> Do you Yahoo!?
> Listen to over 20 online radio stations and watch the latest music videos on Yahoo! Music.
> http://au.launch.yahoo.com
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
More information about the Haskell-Cafe
mailing list