I have a Haskell map containing strings as keys and some lambda functions as elements. For instance:.
-- List of supported Operators -> mapping with functions
ops = Map.fromList [("+", \x y -> x + y),
("-", \x y -> y - x),
("*", \x y -> x * y),
("/", \x y -> y / x)]
I want to write a function that takes as input:
- A string representing the operator ["+", "-", "*", "/"]
- Two numbers
Based on the operator and the ops card, the function will calculate the sum / subtraction / etc. of two numbers.
I tried something like:
(Map.lookup "+" a) 1 2
But it does not work.
Error:
Top level:
No instance for (Show (Integer -> Integer))
arising from use of `print' at Top level
Probable fix: add an instance declaration for (Show (Integer
In a 'do' expression: print it
<interactive>:1:1:
No instance for (Monad ((->) t))
arising from use of `Data.Map.lookup' at <interactive>:1:1-
Probable fix: add an instance declaration for (Monad ((->) t)
In the definition of `it': it = (Data.Map.lookup "+" a) 1 2
... not very helpful to me.
Any suggestions? Thank!
source
share