[Haskell-cafe] Re: Understanding tail recursion and trees

Edsko de Vries devriese at cs.tcd.ie
Thu May 1 08:32:37 EDT 2008


Hi,

Thanks to Miguel for pointing out my silly error. So at least my
understanding of tail recursion is correct :) So then the question
becomes: what *is* the best way to write this function? One version I
can think of is

> ecount :: [Tree] -> Integer -> Integer
> ecount []                  acc = acc
> ecount (Leaf _       : ts) acc = ecount ts $! (acc + 1)
> ecount (Branch t1 t2 : ts) acc = ecount (t1 : t2 : ts) acc

which essentially maintains an explicit stack and runs on all trees. Are
there better ways to do this?

Thanks again and sorry for my mistake,

Edsko


More information about the Haskell-Cafe mailing list