Re: [Haskell-cafe] Construct all possible trees

2007-06-15 Thread Andrew Coppin
Andrew Coppin wrote: trees :: [Int] - [Tree] trees = map fst . (\ts - all_trees 1 (2 * length ts) ts) . map Leaf all_trees :: Int - Int - [Tree] - [(Tree,[Tree])] all_trees n m ts | n m = [] | otherwise = pick ts ++ sub_trees n m ts sub_trees :: Int - Int - [Tree] -

Re: [Haskell-cafe] Construct all possible trees

2007-06-14 Thread Mirko Rahn
I'm afraid, but you are missing a case here. For example the tree Branch (Leaf 1) (Branch (Leaf 3) (Leaf 2)) is not constructed by your program. Correct is insert x t@(Leaf y) = [Branch s t, Branch t s] where s = Leaf x insert x t@(Branch l r) = [Branch l' r | l' - insert x l] ++

[Haskell-cafe] Construct all possible trees

2007-06-14 Thread Andrew Coppin
Well, I eventually came up with this: - data Tree = Leaf Int | Branch Tree Tree deriving Show pick :: [x] - [(x,[x])] pick = pick_from [] pick_from :: [x] - [x] - [(x,[x])] pick_from ks [] = [] pick_from ks xs = (head xs, ks ++ tail xs) : pick_from (ks ++ [head

Re: [Haskell-cafe] Construct all possible trees

2007-06-13 Thread Mirko Rahn
Andrew Coppin wrote: such that all_trees [1,2,3] will yield [ Leaf 1, Leaf 2, Leaf 3, Branch (Leaf 1) (Leaf 2), Branch (Leaf 1) (Leaf 3), Branch (Leaf 2) (Leaf 1), Branch (Leaf 2) (Leaf 3), Branch (Leaf 3) (Leaf 1), Branch (Leaf 3) (Leaf 2), Branch (Branch (Leaf 1) (Leaf 2)) (Leaf 3), Branch

[Haskell-cafe] Construct all possible trees

2007-06-13 Thread Tom Pledger
*Andrew Coppin wrote: * | I'm trying to construct a function | | all_trees :: [Int] - [Tree] | | such that all_trees [1,2,3] will yield : If you write a helper function that takes an N element list, and returns all 2^N ways of dividing those elements into 2 lists, e.g. splits ab --

Re: [Haskell-cafe] Construct all possible trees

2007-06-13 Thread Henning Thielemann
On Thu, 14 Jun 2007, Tom Pledger wrote: *Andrew Coppin wrote: * | I'm trying to construct a function | | all_trees :: [Int] - [Tree] | | such that all_trees [1,2,3] will yield : If you write a helper function that takes an N element list, and returns all 2^N ways of dividing

Re: [Haskell-cafe] Construct all possible trees

2007-06-13 Thread Andrew Coppin
Colin DeVilbiss wrote: On 6/12/07, Andrew Coppin [EMAIL PROTECTED] wrote: Based on the sample output, I'm guessing that the desired output is every tree which, when flattened, gives a permutation of a non-empty subset of the supplied list. This limits the output to trees with up to n leaves.

Re: [Haskell-cafe] Construct all possible trees

2007-06-13 Thread Lennart Augustsson
This doesn't enumerate them in the order you want, but maybe it doesn't matter. module Trees where combinations :: [a] - [[a]] combinations [] = [[]] combinations (x:xs) = combinations xs ++ [ x:xs' | xs' - combinations xs ] data Tree = Leaf Int | Branch Tree Tree deriving (Show) trees

[Haskell-cafe] Construct all possible trees

2007-06-12 Thread Andrew Coppin
I'm trying to construct a function all_trees :: [Int] - [Tree] such that all_trees [1,2,3] will yield [ Leaf 1, Leaf 2, Leaf 3, Branch (Leaf 1) (Leaf 2), Branch (Leaf 1) (Leaf 3), Branch (Leaf 2) (Leaf 1), Branch (Leaf 2) (Leaf 3), Branch (Leaf 3) (Leaf 1), Branch (Leaf 3) (Leaf 2), Branch

Re: [Haskell-cafe] Construct all possible trees

2007-06-12 Thread Colin DeVilbiss
On 6/12/07, Andrew Coppin [EMAIL PROTECTED] wrote: Based on the sample output, I'm guessing that the desired output is every tree which, when flattened, gives a permutation of a non-empty subset of the supplied list. This limits the output to trees with up to n leaves. Branch (Branch (Leaf 1)