Posted to tcl by dkf at Fri Jun 12 13:40:32 GMT 2009view raw

  1. proc fib n {
  2. apply {{a b n} {
  3. if {$n<1} {return $a}
  4. if {$n == 1} {return $b}
  5. tailcall apply [lindex [info level 0] 1] $b [incr a $b] [incr n -1]
  6. }} 0 1 $n
  7. }
  8.  
  9.  

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