1
votes

I am currently learning Haskell and I have run into a problem but I don't really understand what is the problem. When I compile the code below, it tells me "Couldnt match expected type 'integer' with actual type 'Maybe Integer'" for line 17:19 and again the same error for 17:27.

maybe_divide :: Maybe Integer -> Maybe Integer -> Maybe Integer
maybe_divide a b = case valid_div a b of
True  -> Just (a `div` b)
False -> Nothing

valid_div :: Maybe Integer -> Maybe Integer -> Bool
valid_div a b
    | a == Nothing = False
    | b == Nothing = False
    | b == Just 0  = False
    | otherwise = True

Any help would be appreciated. Thanks

1

1 Answers

2
votes

In maybe_divide, the types of a and b are Maybe Integer.... You can not divide Maybe Integers

a `div` b --won't work

You will have to "unwrap" the Maybe values, extract the Integers before you use them in div. (There are a few ways to do this, including pattern matching).