Posted to tcl by kbk at Fri Jun 12 20:20:55 GMT 2009view raw

  1. namespace path ::tcl::mathop
  2. proc fib {n {c {{x} {set x}}}} {
  3. while {$n >= 2} {
  4. set c [list [list x [list n $n] [list c $c]] {
  5. tailcall fib [- $n 2] [list [list y [list x $x] [list c $c]] {
  6. tailcall apply $c [+ $x $y]
  7. }]
  8. }]
  9. incr n -1
  10. }
  11. tailcall apply $c $n
  12. }
  13. for {set i 0} {$i <= 10} {incr i} {
  14. puts [fib $i]
  15. }