All pastes #267000 Raw Edit

Mobile balance

public text v1 · immutable
#267000 ·published 2006-12-04 16:06 UTC
rendered paste body
-- 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