2012-09-23

proc body and args

考慮下面 Tcl 文件裡的示範:
proc printProc {procName} {
    set result [list proc $procName]
    set formals {}
    foreach var [info args $procName] {
        if {[info default $procName $var def]} {
            lappend formals [list $var $def]
        } else {
            # Still need the list-quoting because variable
            # names may properly contain spaces.
            lappend formals [list $var]
        }
    }
    puts [lappend result $formals [info body $procName]]
}

所以我們可以很容易的知道 proc 參數和函式文字內容的資訊。也就是說,我們可以使用 info command 來拿到某個函式的內容(也就是使用 info body $procname 來拿到),並且使用 eval 執行之。

下面是一些練習的嘗試:
proc hello {x} {
    puts $x
}

proc rand {x} {
    set num [expr rand()]
    return [expr $num * $x]
}

proc hello2 {procname x} {
    if {[string compare "hello" $procname]==0} {
        incr x
    }     
    eval [info body $procname]
}   

hello2 hello 3
hello2 rand 10

很神奇吧?雖然我不知道有什麼用處,但是可以直接拿別的 proc 的 function body 來執行,對我而言是很神奇的。

2012-09-20

Tcl/TK 8.6 beta 3 released

Tcl/Tk 8.6b3 RELEASED

這個版本的發佈,讓我相信 Tcl/Tk 8.6 接近完工了,給我一種興奮的感覺(但是看起來,也多了一些和之前版本不相容的情況)。

比較詳細的內容如下:

The following were the main changes in Tcl/Tk 8.6b3.  A complete list
can be found in the changes file at the root of the source tree.  The
more complete ChangeLog is also included with each source release.

This is a beta release of 8.6.  The beta designation means that the
feature set for 8.6 is believed to be complete, and the focus is now
on testing and bug fixing moving quickly toward an 8.6.0 release.
All relevant bug fixes (and some more) up to and including 8.5.12 changes
are included in 8.6b3.  This release is a development release, and should
only be considered for deployment use after considerable testing.

     * [TIP 388] New syntax for extended Unicode: \Uhhhhhhhh

     * [TIP 396] New command: [yieldto]

     * [TIP 398] Non-blocking channels no longer forced to flush on exit
         *** POTENTIAL INCOMPATIBILITY ***

     * [TIP 380] New OO builtin class: [oo::Slot]
         *** POTENTIAL INCOMPATIBILITY ***

     * [TIP 397] New OO standard method: - extends [oo::copy]
         *** POTENTIAL INCOMPATIBILITY ***

     * [TIP 395] New subcommand: [string is entier]

     * [TIP 403] Redefined color names to match Web colors.
         *** POTENTIAL INCOMPATIBILITY ***

     * On Windows, adopt convention that Ctrl-A means Select-All
         *** POTENTIAL INCOMPATIBILITY ***

     * Increased use of virtual events in Tk.
         *** POTENTIAL INCOMPATIBILITY ***

     * [TIP 376] Bundled distribution of sqlite3 3.7.14, tdbcsqlite3-1.0b17

     * Update to Unicode 6.2 support.

     * Bytecode compiled: [catch] [dict with] [lrange] [lreplace]

     * Updated packages:
         - dde 1.4.0b1
                 [TIP 106] New option: [dde execute|poke -binary]
         - http 2.8.4
                 Accept IPv6 URLs;  Make -headers override -type.
         - Itcl 4.0b8
         - msgcat 1.5.0
                 [TIP 404] New commands: [mcflset] [mcflmset]
         - registry 1.3.0
                 fix [registry keys] failure
         - tcltest 2.3.4
         - TclOO 0.7
         - tdbc 1.0b17
         - Thread 2.7b1
                 New command: [thread::cancel]

     * Fixed several clip region issues.

     * Fixed crash in tearoff menus.

     * Made [auto_execok] search match that for [exec].

     * Several fixes to stacked zlib (de)compression channels

     * Mac OS X versions less than 10.4 (Tiger) no longer supported.
         *** POTENTIAL INCOMPATIBILITY ***

2012-09-08

遞迴練習

namespace path {::tcl::mathop ::tcl::mathfunc}

proc fac n {
    if {$n <= 1} {
        return 1;
    } else {
        return [+ $n [fac [- $n 1]]]
    }    
}

簡單的遞迴練習程式,為了多一些變化,所以使用了不透過 expr 的方式。