Fibonacci numbers in Tcl
Example for versions
ActiveTcl 8.5,
JTcl 2.1.0,
Tcl 8.4,
Tcl 8.5.7
This example uses tail recursion to calculate the numbers. eval command allows to evaluate the result of calling fib with given arguments without declaring fib as part of any namespace.
proc fib {f1 f2 n} {
if {$n==0} {
return $f1
} else {
return [eval fib $f2 [expr {$f1 + $f2}] [expr {$n - 1}]]
}
}
set s ""
for {set i 0} {$i < 16} {incr i} {
append s [eval fib 1 1 $i] ", "
}
puts "$s..."
Comments
]]>blog comments powered by Disqus
]]>