[Haskell-beginners] Returning Maybe Float -> Float -> Float

Theodore Lief Gannon tanuki at gmail.com
Mon Feb 8 23:15:20 UTC 2016


This is a great use case for Monad Maybe:

runOperation :: String -> Float -> Float -> Maybe Float
runOperation op x y = do
  op' <- operation op
  return $ op' x y

Or coming at it from the other end, Applicative Maybe:

runOperation' :: String -> Float -> Float -> Maybe Float
runOperation' op x y = operation op <*> Just x <*> Just y


On Mon, Feb 8, 2016 at 6:21 AM, David McBride <toad3k at gmail.com> wrote:

> You need to test to see whether you have a valid operation before you can
> input the 2.0's.  Try this:
>
> main = do
>   x <- getLine
>   putStrLn $ case operation x of
>     Nothing -> "Got nothing."
>     Just op -> show $ op 2.0 2.0
>
>
> On Mon, Feb 8, 2016 at 9:14 AM, Olivier Duhart <olivier.duhart at gmail.com>
> wrote:
>
>> thanks Ulrik,
>>
>> it solves the compilation problem. But now  I have an execution error
>> (with GHCi) :
>>
>> *Main> (operation "+") 2.0 2.0
>>
>> <interactive>:3:1:
>>     Couldn't match expected type `Double -> Double -> t'
>>                 with actual type `Maybe (Float -> Float -> Float)'
>>     Relevant bindings include it :: t (bound at <interactive>:3:1)
>>     The function `operation' is applied to three arguments,
>>     but its type `String -> Maybe (Float -> Float -> Float)'
>>     has only one
>>     In the expression: (operation "+") 2.0 2.0
>>     In an equation for `it': it = (operation "+") 2.0 2.0
>>
>> 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 ?
>>
>> I tried to replace every  Float with Double in my .hs file but still
>> there is a problem
>>
>> *Main> (operation "+") 2.0 2.0
>>
>> <interactive>:3:1:
>>     Couldn't match expected type `Double -> Double -> t'
>>                 with actual type `Maybe (Double -> Double -> Double)'
>>     Relevant bindings include it :: t (bound at <interactive>:3:1)
>>     The function `operation' is applied to three arguments,
>>     but its type `String -> Maybe (Double -> Double -> Double)'
>>     has only one
>>     In the expression: (operation "+") 2.0 2.0
>>     In an equation for `it': it = (operation "+") 2.0 2.0
>>
>> Could you explain me what is the -> t at the end of the expected type ?
>>
>>
>>
>> Le lun. 8 févr. 2016 à 15:07, Ulrik Rasmussen <haskell at utr.dk> a écrit :
>>
>>> On 2016-02-08 14:54, Olivier Duhart wrote:
>>> > 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
>>>
>>> Hi,
>>>
>>> The type String -> Maybe Float -> Float -> Float parenthesizes as
>>>
>>>     String -> (Maybe Float) -> Float -> Float,
>>>
>>> but I think you meant
>>>
>>>     String -> Maybe (Float -> Float -> Float)
>>>
>>>
>>> /Ulrik
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160208/49f9270c/attachment.html>


More information about the Beginners mailing list