Created
April 24, 2012 07:56
-
-
Save jneira/2477668 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RandTree where | |
import Data.Tree | |
import System.Random | |
range=(0,2) | |
probChild:: StdGen -> [Int] | |
probChild g=randomRs range g | |
maxChilds=2 | |
generator :: StdGen -> ([Int],[StdGen]) | |
generator seed= | |
let rs=take maxChilds $ probChild seed | |
s=length $ filter (>0) rs | |
childs=replicate s (snd.next $ seed) | |
in (rs,childs) | |
genTree=unfoldTree generator | |
count=length.flatten | |
main= newStdGen >>= return.count.genTree |
*RandTree> :main
3677535
(one minute)
*RandTree> :main
12714851
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from Data.Tree
flatten :: Tree a -> [a]
flatten t = squish t []
where squish (Node x ts) xs = x:Prelude.foldr squish xs ts