Fibonacci numbers in Tcl
Example for versions
ActiveTcl 8.5,
Tcl 8.5.7
This example uses recursive definition of Fibonacci numbers. Function fib
is defined in namespace tcl::mathfunc
, so that it can be used as math function in expr
expressions.
proc tcl::mathfunc::fib {n} {
if {$n<=1} {
return 1
} else {
return [expr fib([expr {$n - 1}]) + fib([expr {$n - 2}])]
}
}
set s ""
for {set i 0} {$i < 16} {incr i} {
append s [expr fib($i)] ", "
}
puts "$s..."
Comments
]]>blog comments powered by Disqus
]]>