Posted to tcl by dkf at Fri Jun 12 13:40:32 GMT 2009view pretty
proc fib n { apply {{a b n} { if {$n<1} {return $a} if {$n == 1} {return $b} tailcall apply [lindex [info level 0] 1] $b [incr a $b] [incr n -1] }} 0 1 $n }
proc fib n { apply {{a b n} { if {$n<1} {return $a} if {$n == 1} {return $b} tailcall apply [lindex [info level 0] 1] $b [incr a $b] [incr n -1] }} 0 1 $n }
Comments
Posted by miguel at Fri Jun 12 13:44:36 GMT 2009 [text] [code]
slightly better: the body could be if {$n > 1} {tailcall ...} if {$n == 1} {return $b} return $a so that there are fewer tests in most calls