1tailcall(n)                  Tcl Built-In Commands                 tailcall(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       tailcall - Replace the current procedure with another command
9

SYNOPSIS

11       tailcall command ?arg ...?
12______________________________________________________________________________
13

DESCRIPTION

15       The tailcall command replaces the currently executing procedure, lambda
16       application, or method with another command. The  command,  which  will
17       have  arg  ... passed as arguments if they are supplied, will be looked
18       up in the current namespace context, not in the  caller's.  Apart  from
19       that difference in resolution, it is equivalent to:
20
21              return [uplevel 1 [list command ?arg ...?]]
22
23       This command may not be invoked from within an uplevel into a procedure
24       or inside a catch inside a procedure or lambda.
25

EXAMPLE

27       Compute the factorial of a number.
28
29              proc factorial {n {accum 1}} {
30                  if {$n < 2} {
31                      return $accum
32                  }
33                  tailcall factorial [expr {$n - 1}] [expr {$accum * $n}]
34              }
35
36       Print the elements of a list with alternating  lines  having  different
37       indentations.
38
39              proc printList {theList} {
40                  if {[llength $theList]} {
41                      puts "> [lindex $theList 0]"
42                      tailcall printList2 [lrange $theList 1 end]
43                  }
44              }
45              proc printList2 {theList} {
46                  if {[llength $theList]} {
47                      puts "< [lindex $theList 0]"
48                      tailcall printList [lrange $theList 1 end]
49                  }
50              }
51

SEE ALSO

53       apply(n), proc(n), uplevel(n)
54

KEYWORDS

56       call, recursion, tail recursion
57
58
59
60Tcl                                   8.6                          tailcall(n)
Impressum