Measure the work of producing the entire output list, with constant-cost labels. Count height in levels, so a leaf has height one and . A perfect ternary tree with levels has
vertices. This is the finite geometric series formula, used without a new proof.
The three recursive calls cost . Ordinary concat = foldr (++) [] copies each immediate child result once, and each result has elements. Consequently the nonrecursive work is , giving
Unroll this recurrence relation. At level , the cost is , and there are internal levels, followed by leaves. Therefore
For this is conventionally written ; in terms of total size it is . The extra height factor comes from copying labels at successive ancestors, even under lazy evaluation when the whole output is demanded. Merely producing its first constructor is a different cost question. If height instead counts edges, the source's height means levels; shifting the index by one leaves the boxed asymptotic bound unchanged. Part (c)'s suffix-passing implementation avoids this repeated copying.

Articles by others on the same topic (0)

There are currently no matching articles.