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

Ulrik Rasmussen haskell at utr.dk
Mon Feb 8 14:07:40 UTC 2016


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


More information about the Beginners mailing list