RosettaCodeData/Task/Anonymous-recursion/Tcl/anonymous-recursion-1.tcl

11 lines
304 B
Tcl

proc fib n {
# sanity checks
if {[incr n 0] < 0} {error "argument may not be negative"}
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]]}
}} $n
}