tailcall(n)
π« tailcall(n) π«
ok so this is a Tcl Built-In Command bestie, and it's genuinely one of the slayest little tricks in the whole interpreter. sit down, we're doing recursion the efficient way π«Ά
β¨ NAME β¨
tailcall β replace the current procedure with another command π
πΈ SYNOPSIS πΈ
tailcall command ?arg ...?
π¦ DESCRIPTION π¦
okay so here's the vibe: tailcall swaps out the procedure, lambda application, or method that's currently running and puts another command in its place. main character energy, honestly.
That command β and the arg ... you hand it, if you hand it any β gets looked up in the current namespace context, NOT in the caller's. that's the one lil plot twist. Apart from that difference in resolution, it is equivalent to:
return [uplevel 1 [list command ?arg ...?]]
and that's the whole thing fr fr. no cap. π
π¨ okay important caveat bestie: this command may not be invoked from within anuplevelinto a procedure, or inside acatchinside a procedure or lambda. it's giving forbidden. don't do it. π
π₯ EXAMPLE π₯
Compute the factorial of a number π
proc factorial {n {accum 1}} {
if {$n < 2} {
return $accum
}
tailcall factorial [expr {$n - 1}] [expr {$accum * $n}]
}
the way that tailcall just re-invokes itself and never grows the stack?? obsessed. periodt. β¨
Print the elements of a list with alternating lines having different indentations π
proc printList {theList} {
if {[llength $theList]} {
puts "> [lindex $theList 0]"
tailcall printList2 [lrange $theList 1 end]
}
}
proc printList2 {theList} {
if {[llength $theList]} {
puts "< [lindex $theList 0]"
tailcall printList [lrange $theList 1 end]
}
}
look at them passing the baton back and forth like it's a relay race π > then < then > then <β¦ iconic behavior honestly.
π SEE ALSO π
π KEYWORDS π
- call
- recursion
- tail recursion
ok bye bestie, go write some stack-safe recursion and think of me π β¨
Tcl 8.6 β tailcall(n) π mwah, go slay π«Ά
π¦ where this page comes from
- alpine 3.22: tcl-doc 8.6.16-r0 Β· TCL
- alpine 3.23: tcl-doc 8.6.17-r0 Β· TCL
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl-doc 8.6.17-r1 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- arch rolling: tcl 8.6.16-1 Β· TCL
- centos-stream 10: tcl-doc 1:8.6.13-4.el10 Β· TCL AND GPL-3.0-or-later WITH bison-exception-2.2 AND BSD-3-Clause
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms