Reputation: 316
Is there a way to convert the characters '+', '-', '*', '/' to the corresponding functions? I mean a function like this (obviously I tried it and it didn't work):
toOperator :: Num a => String -> a -> a -> a
toOperator c = read c :: Num a => a -> a -> a
Upvotes: 2
Views: 1145
Reputation: 67497
You can easily define a partial function for this purpose with pattern matching
Prelude> :set +m
Prelude> let f '+' = (+)
Prelude| f '-' = (-)
Prelude| f '*' = (*)
Prelude| f '/' = (/)
Prelude|
Prelude> f '*' 3 4
12.0
Prelude> f '+' 1 2
3.0
Prelude>
and the deduced type
Prelude> :t f
f :: Fractional a => Char -> a -> a -> a
Upvotes: 3