I\'m pretty new to Haskell and I\'m trying to work out how to traverse a n-ary tree. As output I\'m looking to get a list of Leaf values (as the branches have no value), so for
You're on the right lines with map
, but after traversing each subtree you want to concat
the resulting lists together. There's also no point breaking off the first element of the list with the (x:xs)
pattern when using map
. I'd write this as:
travTree (Branch xs) = concatMap travTree xs
(But beware; I haven't tested that! However I often find my "infinite type a = [a]" problems are caused by a map
where concatMap
is needed.)