<div dir="ltr">This is a great use case for Monad Maybe:<div><br></div><div><div>runOperation :: String -> Float -> Float -> Maybe Float</div><div>runOperation op x y = do</div><div> op' <- operation op</div><div> return $ op' x y</div></div><div><br></div><div>Or coming at it from the other end, Applicative Maybe:</div><div><br></div><div><div>runOperation' :: String -> Float -> Float -> Maybe Float</div><div>runOperation' op x y = operation op <*> Just x <*> Just y</div></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 8, 2016 at 6:21 AM, David McBride <span dir="ltr"><<a href="mailto:toad3k@gmail.com" target="_blank">toad3k@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">You need to test to see whether you have a valid operation before you can input the 2.0's. Try this:<br><br>main = do<br> x <- getLine<br> putStrLn $ case operation x of<br> Nothing -> "Got nothing."<br> Just op -> show $ op 2.0 2.0<br><br></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 8, 2016 at 9:14 AM, Olivier Duhart <span dir="ltr"><<a href="mailto:olivier.duhart@gmail.com" target="_blank">olivier.duhart@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">thanks Ulrik,<div><br></div><div>it solves the compilation problem. But now I have an execution error (with GHCi) :</div><div><br></div><div><div>*Main> (operation "+") 2.0 2.0</div><div><br></div><div><interactive>:3:1:</div><div> Couldn't match expected type `Double -> Double -> t'</div><div> with actual type `Maybe (Float -> Float -> Float)'</div><div> Relevant bindings include it :: t (bound at <interactive>:3:1)</div><div> The function `operation' is applied to three arguments,</div><div> but its type `String -> Maybe (Float -> Float -> Float)'</div><div> has only one</div><div> In the expression: (operation "+") 2.0 2.0</div><div> In an equation for `it': it = (operation "+") 2.0 2.0</div></div><div><br></div><div>As I understand it ghci guess that the two 2.0 parameters to my (operation "+") are Double and the function returned by operation expects Float instead, How to solve this ?</div><div><br></div><div>I tried to replace every Float with Double in my .hs file but still there is a problem</div><div><br></div><div><div>*Main> (operation "+") 2.0 2.0</div><div><br></div><div><interactive>:3:1:</div><div> Couldn't match expected type `Double -> Double -> t'</div><div> with actual type `Maybe (Double -> Double -> Double)'</div><div> Relevant bindings include it :: t (bound at <interactive>:3:1)</div><div> The function `operation' is applied to three arguments,</div><div> but its type `String -> Maybe (Double -> Double -> Double)'</div><div> has only one</div><div> In the expression: (operation "+") 2.0 2.0</div><div> In an equation for `it': it = (operation "+") 2.0 2.0</div></div><div><br></div><div>Could you explain me what is the -> t at the end of the expected type ?</div><div><div><div><br></div><div><br></div><br><div class="gmail_quote"><div dir="ltr">Le lun. 8 févr. 2016 à 15:07, Ulrik Rasmussen <<a href="mailto:haskell@utr.dk" target="_blank">haskell@utr.dk</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 2016-02-08 14:54, Olivier Duhart wrote:<br>
> hello,<br>
><br>
> I am starting with Haskell and trying some little exercices on my own. I<br>
> successfully implemented a reverse polish notation evaluator and I want<br>
> to improve it a little using *Maybe*<br>
> *<br>
> *<br>
> All i want is to implement a function that can returned available<br>
> functions according to its string name<br>
> i.e return (+) when gived "+"<br>
> I also want to return Nothing if the operation is not available (not<br>
> implemented yet).<br>
> So my function should Maybe return a (float -> float -> float)<br>
><br>
> My current implementation is<br>
><br>
> operation :: String -> Maybe Float -> Float -> Float<br>
> operation op<br>
> | op == "+" = Just (+)<br>
> | op == "-" = Just (-)<br>
> | op == "*" = Just (*)<br>
> | op == "/" = Just (/)<br>
> | otherwise = Nothing<br>
><br>
><br>
> but it failed to compile with the following error :<br>
><br>
> rpn.hs:64:18:<br>
> Couldn't match expected type `Maybe Float -> Float -> Float'<br>
> with actual type `Maybe a0'<br>
> In the expression: Nothing<br>
> In an equation for `operation':<br>
> operation op<br>
> | op == "+" = Just (+)<br>
> | op == "-" = Just (-)<br>
> | op == "*" = Just (*)<br>
> | op == "/" = Just (/)<br>
> | otherwise = Nothing<br>
><br>
><br>
> I don't understand the error :( Do I have to explicitly type the Nothing<br>
> return ?<br>
><br>
> Could you guide me to a solution or explain me what I am doing wrong,<br>
> please ?<br>
><br>
> Thanks in advance<br>
><br>
> Olivier<br>
<br>
Hi,<br>
<br>
The type String -> Maybe Float -> Float -> Float parenthesizes as<br>
<br>
String -> (Maybe Float) -> Float -> Float,<br>
<br>
but I think you meant<br>
<br>
String -> Maybe (Float -> Float -> Float)<br>
<br>
<br>
/Ulrik<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div></div></div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>