-- Test in one pass whether a Calder mobile is balanced -- (problem due to Olivier Danvy; via Per Vognsen) module Balance where data Mobile a = Leaf a | Branch a (Mobile a) a (Mobile a) deriving (Eq, Show) balance (Leaf w) = Just w balance (Branch d1 m1 d2 m2) = case (balance m1, balance m2) of (Just w1, Just w2) | w1 * d1 == w2 * d2 -> Just (w1 + w2) _ -> Nothing