data DividedResult = Result Integer | DividedByZero deriving Show divByA :: Integral a => a -> a -> DividedResult divByA num denom = go num denom 0 where go n d count | d == 0 = DividedByZero -- A | d < 0 = case divByA n (-d) of -- B DividedByZero -> DividedByZero Result r -> Result (-r) | n < 0 = case divByA (-n) d of -- C DividedByZero -> DividedByZero Result r -> Result (-r) | n < d = Result count -- D | otherwise = go (n - d) d (count + 1) -- E
This is someone else’s solution to an exercise in a book. It took me some time but I think understand most of it. I don’t get the
DividedByZero -> DividedByZero in guards B & C. As I understand it, B says,
- "when d < 0
- && divBy a is called with a positive numerator and negative denominator,
Now that I’m looking at it again, that last point doesn’t seem clear to me.
I really dont get why B & C have
DividedByZero -> DividedByZero. I would have guessed
Integer -> Integer.