Jon Fairbairn wrote:
Trees with all the elements of a list in that order:
the_trees [x] = [Leaf x]
the_trees l = zipWith Branch (concat (map the_trees (tail $ inits l)))
(concat (map the_trees (tail $ tails l)))
Sorry, but this problem seems to trigger incorrect
Mirko Rahn [EMAIL PROTECTED] writes:
Jon Fairbairn wrote:
Trees with all the elements of a list in that order:
the_trees [x] = [Leaf x]
the_trees l = zipWith Branch (concat (map the_trees (tail $ inits l)))
(concat (map the_trees (tail $ tails l)))
Sorry,
Andrew Coppin wrote:
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),
apfelmus wrote:
Explanation and the code:
import Data.List
import Control.Applicative
import qualified Data.Foldable as Foldable
import Data.Traversable as Traversable
import Control.Monad.State
data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving (Show)
Mirko Rahn wrote:
apfelmus wrote:
data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving (Show)
permTrees xs = concat . takeWhile (not . null) . map
(flip evalStateT xs . Traversable.sequence) $ trees select
where
select = StateT $ \xs -
Jon Fairbairn wrote:
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
Andrew Coppin [EMAIL PROTECTED] writes:
Jon Fairbairn wrote:
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),
Andrew Coppin [EMAIL PROTECTED] writes:
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),