
On 2/14/06, Steve Schafer
isChild :: Int -> Node -> Bool isChild i (Nd j _) = (j > i) isChild _ _ = False
prepend :: Int -> [Node] -> [Node] prepend i [] = [Nd i []] prepend i ns = (Nd i f):s where (f,s) = span (isChild i) ns
unflatten :: [Int] -> [Node] unflatten ns = foldr prepend [] ns
The following seemed a little more natural to me: unflatten [] = [] unflatten (x:xs) = (Node x $ unflatten xh) : unflatten xt where (xh, xt) = span (>x) xs That is, a node containing the first item and the hierarchy of all of its children, followed by the hierarchies of its siblings. Is [0,2,1,2] invalid input? The behavior of both versions puts the first 2 and the 1 as children of the root, but the second 2 is a child of the 1. Colin DeVilbiss