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] -
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] ++
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
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
*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 --
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
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.
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
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
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)
10 matches
Mail list logo