I was trying to implement a
foldTree function to generate a balanced binary tree from a list of values using
foldr (Question 2 here), but the resulting solution is not really written in a proper manner:
data Tree a = Leaf | Node Int (Tree a) a (Tree a) deriving (Show,Eq) foldTree::[a]-> Tree a foldTree a=_foldTree a 0 ((getHeight (length a))-1) (length a) where _foldTree::[a]->Int->Int->Int->Tree a _foldTree a n h l | n>=l = Leaf | otherwise = Node h (_foldTree a (2*n+1) (h-1) l ) (a!!n) (_foldTree a (2*n+2) (h-1) l) getHeight::Int->Int getHeight n=ceiling(log (fromIntegral(n))/log 2)
The solution suffers from precision problems. And I think I can use some high level abstractions which I have been missing.
I am just making the heap (sort of, without ordering) out of the list.