The following tail-recursion example is taken verbatim from Chris Smith's Programming F# 3.0. If this is not the appropriate forum for this post, I will gladly move the post to the appropriate place. I did not post this in SO, because I do not have a problem to solve.
let printListRev list =
let rec printListRevTR list cont =
match list with
// For an empy list, execute the continuation
| [] -> cont()
// For other lists, add printing the current
// node as part of the continuation.
| hd :: tl ->
printListRevTR tl (fun () -> printf "%d " hd
cont())
printListRevTR list (fun () -> printfn "Done!");;
- What exactly is
cont()
continuing. I believe I understand the concept thatcont()
is encapsulating the remaining work to be done, but I am still a little confused about what is going on. - This SO post contains use of the keyword
loop
in @Juliet 's answer. I am confused as to why loop is used in that answer and not used in the example above.
loop
andcont
are not keywords, but are variable/method names. Tail-call recursion is a hard concept to understand, especially if you're new to F#. \$\endgroup\$