Proposal: improve the Data.Tree API

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


Hello,

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

http://hackage.haskell.org/package/debian-3.81/docs/Debian-Apt-Dependencies.html#g:3

http://hackage.haskell.org/package/hledger-lib-0.22.1/docs/Hledger-Utils.html#v:subtreeat

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)
occurrence
-- 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
that
-- 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
welcomed.

Previous discussion:
https://github.com/haskell/containers/issues/39#issuecomment-35799628

Discussion period: 2 weeks

Cheers,
João
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20140224/1a0e2792/attachment-0001.html>


More information about the Libraries mailing list