[Haskell-cafe] testing for exceptions

Roel van Dijk vandijk.roel at gmail.com
Sat Oct 31 13:56:47 UTC 2015


You are even closer now!

What the type checker is trying to tell you is that it doesn't know how to
raise Maybe Integer to some power.

You apply the ^ operator to two values, f2Maybe (n `div` 2) and 2. Let us
give them names:

let a = f2Maybe (n `div` 2) :: Maybe Integer
    b = 2 :: Int
in a ^ b

You see that the type of a is Maybe Integer. What does this mean? There are
only 2 cases to consider. You have Just an integer or you have Nothing.

You can use the case construct to write the code for both cases.

f2Maybe :: Integer -> Maybe Integer
f2Maybe n
   | n > 0  = Nothing
   | n == 0  = Just 1
   | even n = case f2Maybe (n `div` 2) of
                Just x -> <fill in>
                Nothing -> <fill in>
   | odd n  = case f2Maybe (n `div` 2) of
                Just x -> <fill in>
                Nothing -> <fill in>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151031/d95d5e69/attachment.html>


More information about the Haskell-Cafe mailing list