## Tcl8.6.17/Tk8.6.17 Documentation > Tcl Commands > tailcall

### Tcl/Tk Applications | Tcl Commands | Tk Commands | [incr Tcl] Package Commands | SQLite3 Package Commands | TDBC Package Commands | tdbc::mysql Package Commands | tdbc::odbc Package Commands | tdbc::postgres Package Commands | tdbc::sqlite3 Package Commands | Thread Package Commands | Tcl C API | Tk C API | [incr Tcl] Package C API | TDBC Package C API

### NAME

tailcall — 現在の手続函数を他の命令で置き換える Replace the current procedure with another command

### 構文 SYNOPSIS

tailcall command ?arg ...?

### 説明 DESCRIPTION

この tailcall 命令は､現在実行している手続函数や､λ式や､他の手順を別の命令に置き換える｡この command は arg ... が提供されると､引数として引渡され､それは､呼び出し側の名前空間の文脈からではなく､現在の名前空間の文脈から捜索される｡解答の差異を別にすると､以下のものと等価である｡ The tailcall command replaces the currently executing procedure, lambda application, or method with another command. The command, which will have arg ... passed as arguments if they are supplied, will be looked up in the current namespace context, not in the caller's. Apart from that difference in resolution, it is equivalent to:

    return [uplevel 1 [list command ?arg ...?]]

この命令は手続函数内の uplevel 内部または､手続函数やλ内部の catch からは起動されない｡ This command may not be invoked from within an uplevel into a procedure or inside a catch inside a procedure or lambda.

### 用例 EXAMPLE

数値から階乗を計算する｡ Compute the factorial of a number.

    proc factorial {n {accum 1}} {
        if {$n < 2} {
            return $accum
        }
        tailcall factorial [expr {$n - 1}] [expr {$accum * $n}]
    }

異なる段下げを有する交代々々の行からなるリストの要素を印刷する｡ Print the elements of a list with alternating lines having different indentations.

    proc printList {theList} {
        if {[llength $theList]} {
            puts "> [lindex $theList 0]"
            tailcall printList2 [lrange $theList 1 end]
        }
    }
    proc printList2 {theList} {
        if {[llength $theList]} {
            puts "< [lindex $theList 0]"
            tailcall printList [lrange $theList 1 end]
        }
    }

### SEE ALSO

apply, proc, uplevel

### KEYWORDS

call, recursion, tail recursion
Copyright © 1993 The Regents of the University of California.
Copyright © 1994-1996 Sun Microsystems, Inc.