Proposal: improve the Data.Tree API

João Cristóvão jmacristovao at
Mon Feb 24 10:39:17 UTC 2014


The Data.Tree API seems rather poor. Some research on hackage shows some
additional functions being defined in very unrelated packages:

I propose the addition of the following functions, that seem rather straigh
forward to me:

-- | get the sub-tree rooted at the first (left-most, depth-first)
-- of the specified node value
lookupTree :: Eq a => a -> Tree a -> Maybe (Tree a)

-- | get the sub-tree rooted at the first (left-most, depth-first) value
-- matches the provided condition
findTree :: (a -> Bool) -> Tree a -> Maybe (Tree a)

-- | keep only the elements that match the provided condition
filter :: (a -> Bool) -> Tree a -> Tree a

The 'Tree' is appended in the name, to distinguish from the Foldable
instances, that return the value, and not a sub-tree.

Additionally, the following two functions might also be useful (even if the
implementation is very simple in the second case):
-- | get the sub-tree for the specified node value in the first tree in
-- forest in which it occurs.
lookupTreeInForest :: Eq a => a -> [Tree a] -> Maybe (Tree a)

-- | Length of the tree
length :: Tree a -> Int

There are probably more useful functions to add, so suggestions are

Previous discussion:

Discussion period: 2 weeks

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Libraries mailing list