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
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to