[Haskell-cafe] testing for exceptions

Roelof Wobben r.wobben at home.nl
Sat Oct 31 13:08:34 UTC 2015


Op 31-10-2015 om 13:44 schreef Darren Grant:
> You're very close. Take a look at the even clause: Let (k :: Integer), 
> and consider how the type of the expression producing the error,
>
>   Just (f2Maybe k)
>
> differs from the type of the expression,
>
>   (f2Maybe k)
>
> Cheers,
> Darren
>

Just (f2Maybe k) is type Maybe (Maybe Integer)
f2Maybe k is type Maybe Integer

But when I change the code to this :

f2Maybe :: Integer -> Maybe Integer
f2Maybe n
    | n > 0  = Nothing
    | n == 0  = Just 1
    | even n = f2Maybe ( n `div` 2) ^ 2
    | odd n  = (f2Maybe ( n `div` 2) ^ 2) * 2

Then I see this error message :

No instance for (Num (Maybe Integer)) arising from a use of ‘^’
     In the expression: f2Maybe (n `div` 2) ^ 2
     In an equation for ‘f2Maybe’:
         f2Maybe n
           | n > 0 = Nothing
           | n == 0 = Just 1
           | even n = f2Maybe (n `div` 2) ^ 2
           | odd n = (f2Maybe (n `div` 2) ^ 2) * 2
Failed, modules loaded: none.


No instance for (Num (Maybe Integer)) arising from a use of ‘^’
     In the expression: f2Maybe (n `div` 2) ^ 2
     In an equation for ‘f2Maybe’:
         f2Maybe n
           | n > 0 = Nothing
           | n == 0 = Just 1
           | even n = f2Maybe (n `div` 2) ^ 2
           | odd n = (f2Maybe (n `div` 2) ^ 2) * 2
Failed, modules loaded: none.

No instance for (Num (Maybe Integer)) arising from a use of ‘^’
     In the expression: f2Maybe (n `div` 2) ^ 2
     In an equation for ‘f2Maybe’:
         f2Maybe n
           | n > 0 = Nothing
           | n == 0 = Just 1
           | even n = f2Maybe (n `div` 2) ^ 2
           | odd n = (f2Maybe (n `div` 2) ^ 2) * 2
Failed, modules loaded: none.



More information about the Haskell-Cafe mailing list