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

Santoemma Enrico Enrico.Santoemma at beta80group.it
Mon Sep 19 09:42:42 EDT 2005


Yeah! I've been struggling on concatMap for a noon and now here is the solution I've been unable to find.

Thank you very much
Enrico

> -----Messaggio originale-----
> Da: J. Garrett Morris [mailto:trevion at gmail.com]
> Inviato: lunedì 19 settembre 2005 15.26
> A: Santoemma Enrico
> Cc: Haskell-Cafe (E-mail)
> Oggetto: Re: [Haskell-cafe] How to get all the paths of a tree
> 
> 
> In this case, I think all you need is:
> 
> > type Path = [String]
> 
> > allPaths :: Path -> Tree -> [Path]
> > allPaths p (Node s []) = [s:p]
> > allPaths p (Node s subTrees) = concatMap (allPaths (s:p)) subTrees
> 
> > paths = map reverse . allPaths []
> 
> In your test case, "root" will be at the beginning of each path
> produced, but you can map tail over the list if you don't want it.
> 
>  /g
> 


More information about the Haskell-Cafe mailing list