a pastebin project

Mobile balance

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

advertising

Create a Paste

Please enter your new post below (or upload a file instead):





Please note that information posted here will not expire by default. If you want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords.

worth-right
fantasy-obligation