Tcl 8.5.7

Version of implementation Tcl of programming language Tcl

A version of Tcl implementation released in 2009.

Examples:

Fibonacci numbers - Tcl (271):

This example uses iterative definition of Fibonacci numbers. lassign assigns successive elements from the list passed as first argument (created by [list ...]) to the variables given by the next arguments (fib1 and fib2 in this case). This command was moved to language core in Tcl 8.5; before that it was part of TclX package.

set fib1 0
set fib2 1
set s ""
for {set i 0} {$i < 16} {incr i} {
    lassign [list $fib2 [incr fib2 $fib1]] fib1 fib2
    append s "$fib1, "
}
puts "$s..."

Fibonacci numbers - Tcl (272):

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..."

Hello, World! - Tcl (215):

puts "Hello, World!"

Factorial - Tcl (273):

This example uses iterative factorial definition. In Tcl 8.4 values of factorials starting with 13! are calculated incorrectly due to overflow. In later versions and other implementations all values are correct.

set fact 1
for {set i 0} {$i <= 16} {incr i} {
    puts "$i! = $fact"
    set fact [expr {$fact * ($i + 1)}]
}

Quadratic equation - Tcl (274):

set A [gets stdin]
if {$A==0} { 
    puts "Not a quadratic equation."; 
    return 
}
set B [gets stdin]
set C [gets stdin]
set D [expr {$B*$B-4*$A*$C}]
set r [expr {-0.5*$B/$A}]
set i [expr {0.5*sqrt(abs($D))/$A}]
if {$D==0} {
    puts "x = $r"
} elseif {$D>0} {
    puts "x1 = [expr {$r+$i}]"
    puts "x2 = [expr {$r-$i}]"
} else {
    puts "x1 = ($r, $i)"
    puts "x2 = ($r, [expr {-$i}])"
}

CamelCase - Tcl (315):

This example shows character-wise string processing. A regular expression is used to check whether current character is a letter.

set S [gets stdin]
set S [string tolower $S]
set L [string length $S]
set lastSpace 1
set cc ""
for {set i 0} {$i < $L} {incr i} {
    set letter [string index $S $i]
    if { [string match {[a-z]} $letter] } {
        if { $lastSpace == 1 } { set letter [string toupper $letter] }
        append cc $letter
        set lastSpace 0
    } else {
        set lastSpace 1
    }
}
puts $cc

Fibonacci numbers - Tcl (386):

This example uses iterative definition of Fibonacci numbers.

set fib1 0
set fib2 1
set s ""
for {set i 0} {$i < 16} {incr i} {
    set fib3 [expr {$fib1 + $fib2}]
    set fib1 $fib2
    set fib2 $fib3
    append s "$fib1, "
}
puts "$s..."

Fibonacci numbers - Tcl (387):

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..."