Jump to content

Anonymous recursion: Difference between revisions

→‎{{header|Tcl}}: some descriptive text
(→‎Tcl: Added implementation)
(→‎{{header|Tcl}}: some descriptive text)
Line 64:
 
=={{header|Tcl}}==
This solution uses Tcl 8.5's lambda terms, extracting the current term from the call stack using introspection (storing it in a local variable only for convenience, with that ''not'' in any way being the name of the lambda term; just what it is stored in, and only as a convenience that keeps the code shorter). The lambda terms are applied with the <code>apply</code> command.
<lang tcl>proc fib n {
# sanity checks
Line 69 ⟶ 70:
apply {x {
if {$x < 2} {return $x}
# Extract the lambda term from the stack introspector for brevity
set f [lindex [info level 0] 1]
expr {[apply $f [incr x -1]] + [apply $f [incr x -1]]}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.