[Haskell-beginners] Returning Maybe Float -> Float -> Float
Olivier Duhart
olivier.duhart at gmail.com
Mon Feb 8 13:54:43 UTC 2016
hello,
I am starting with Haskell and trying some little exercices on my own. I
successfully implemented a reverse polish notation evaluator and I want to
improve it a little using *Maybe*
All i want is to implement a function that can returned available
functions according to its string name
i.e return (+) when gived "+"
I also want to return Nothing if the operation is not available (not
implemented yet).
So my function should Maybe return a (float -> float -> float)
My current implementation is
operation :: String -> Maybe Float -> Float -> Float
operation op
| op == "+" = Just (+)
| op == "-" = Just (-)
| op == "*" = Just (*)
| op == "/" = Just (/)
| otherwise = Nothing
but it failed to compile with the following error :
rpn.hs:64:18:
Couldn't match expected type `Maybe Float -> Float -> Float'
with actual type `Maybe a0'
In the expression: Nothing
In an equation for `operation':
operation op
| op == "+" = Just (+)
| op == "-" = Just (-)
| op == "*" = Just (*)
| op == "/" = Just (/)
| otherwise = Nothing
I don't understand the error :( Do I have to explicitly type the Nothing
return ?
Could you guide me to a solution or explain me what I am doing wrong,
please ?
Thanks in advance
Olivier
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160208/ffd5fb06/attachment.html>
More information about the Beginners
mailing list