All pastes #329161 Raw Edit

Miscellany

public text v1 · immutable
#329161 ·published 2007-01-26 14:51 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