Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- small experiment with enumerating binary trees
- data T = B T T | L deriving (Eq)
- instance Show T where
- show L = "."
- show (B l r) = "(" ++ show l ++ show r ++ ")"
- buildTs :: Int -> [T]
- buildTs 0 = [L]
- buildTs n = [B l r | i <- [0..n-1],
- l <- buildTs i,
- r <- buildTs (n-1-i) ]
- randomT :: (MonadRandom m) => Int -> m T
- randomT 0 = return L
- randomT n = do
- i <- getRandomR (0, n-1)
- l <- randomT i
- r <- randomT (n-1-i)
- return $ B l r
- main :: IO ()
- main = do
- let gen = mkStdGen 42
- let t = evalRand (randomT 4) gen
- print t
- -- prints ((..)((..).))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement