Wednesday, September 1, 2010

Recursive call into Tree structure

let rec f2 tree cont =
    match tree with
        | EmptyTree -> cont([])
        | Tree(l, n, r) ->
            f2 l (fun lSum ->
                    f2 r (fun rSum->cont(lSum@[n]@rSum)))

Use continuation to implement the tree recursive function. This way is a better approach because it won't generate "stack over flow" exception.

Remember when you debug this in Visual Studio, remember to check the "Generate tail calls" at "build" tab when you open the project property.

No comments: