[Haskell-cafe] How to get all the paths of a tree

Tomasz Zielonka tomasz.zielonka at gmail.com
Mon Sep 19 09:05:54 EDT 2005

On Mon, Sep 19, 2005 at 12:52:47PM +0200, Rene de Visser wrote:
> -- This function can effectively only be used bottom up, because the only  input
> -- parameter to func comes from the call "(map (treeFold func))", i.e. the rest of
> -- the tree. We can only tell when are are at a leaf ( [b] is empty ).
> -- Add in the other tree fold here!
> treeFold :: (a -> [b] -> b) -> Tree a -> b
> treeFold func (Node header list) = func header (map (treeFold func) list)

You can define paths with treeFold in this way:

paths t =
        (\a fs p ->
            let p' = p ++ [a] in
            if null fs
                then [p']
                else concatMap ($ p') fs)

So treeFold can be used top-down :-)

Best regards

More information about the Haskell-Cafe mailing list