I want to generate all possible trees from an int list [Int] -> [T]
but I generate only one tree.
1 1
2 2
3 5
4 14
5 42
like these Catalan numbers. If my list size is 3, I want to generate 5 possible trees, if 4 — 14 possible trees.
Code:
data T = N T T | L Int deriving (Show)
toT :: [Int] -> T
toT [] = L 0
toT [n] = L n
toT ns = T (toT (take mid ns)) (toT (drop (mid+1) ns))
where
mid = length ns div 2
for example: toT [1..3]
output: N (L 1) (N (L 2) (L 3))
and N (N (L 1) (L 2)) (L 3)
.
now ı did like this
toTree [] = error "!!"
toTree [n] = Leaf n
toTree ns = Node leftTree rightTree
where
leftTree = toTree $ take (length(ns)-1) ns
rightTree = toTree $ drop (length(ns)-1) ns` ı want ns length contiue descend one point recursive but ı didnt
How can ı do that ? in recursive ı will send same list but length wil be descend ı sent [1,2,3] size 3 again ı sent [1,2,3] length 2