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

19 Sep
2005
19 Sep
'05
9:42 a.m.
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@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
7184
Age (days ago)
7184
Last active (days ago)
0 comments
1 participants
participants (1)
-
Santoemma Enrico