how do i use the input as an operator? this is my code:
applyOperator :: IO()
applyOperator = do
putStrLn "Input first Integer: "
num1 <- getLine
putStrLn "Input operator: "
op <- getLine
putStrLn "Input second Integer: "
num2 <- getLine
let solution = (read op :: Num) num1 num2
putStrLn solution
ghci gives me this error:
* Expecting one more argument to `Num'
Expected a type, but `Num' has kind `* -> Constraint'
* In an expression type signature: Num
In the expression: read op :: Num
In the expression: (read op :: Num) num1 num2
i dont really know what ghci is trying to tell me with that.
i've also tried to write line 9 like this:
let solution = num1 ´(read op :: Num)´ num2
is it wrong to try to convert op to the Num type? is op a string when i use <- getLine?
thank you
Numis a typeclass, not a type, henceread op :: Nummakes not much sense. Furthermore parsing to a function is not possible. You can work with a lookup table for example where you map"+"to(+), etc.readreturns something with aReadinstance. Unless there is aReadinstance ofNum a => a -> a -> a, you can't usereadlike this. (Theoretically, you could write such an instance yourself, but as an orphaned instance--one not defined in either the module where the type class is defined or in the module where the type is defined--it's not considered a good idea.)