Debian Codegraph @

doc/tailcall.n

raw
1 '\"
2 '\" Copyright (c) 1993 The Regents of the University of California.
3 '\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4 '\"
5 '\" See the file "license.terms" for information on usage and redistribution
6 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 '\"
8 .TH tailcall n 8.6 Tcl "Tcl Built-In Commands"
9 .so man.macros
10 .BS
11 '\" Note: do not modify the .SH NAME line immediately below!
12 .SH NAME
13 tailcall \- Replace the current procedure with another command
14 .SH SYNOPSIS
15 \fBtailcall \fIcommand\fR ?\fIarg ...\fR?
16 .BE
17 .SH DESCRIPTION
18 .PP
19 The \fBtailcall\fR command replaces the currently executing procedure, lambda
20 application, or method with another command. The \fIcommand\fR, which will
21 have \fIarg ...\fR passed as arguments if they are supplied, will be looked up
22 in the current namespace context, not in the caller's. Apart from that
23 difference in resolution, it is equivalent to:
24 .PP
25 .CS
26 return [uplevel 1 [list \fIcommand\fR ?\fIarg ...\fR?]]
27 .CE
28 .PP
29 This command may not be invoked from within an \fBuplevel\fR into a procedure
30 or inside a \fBcatch\fR inside a procedure or lambda.
31 '\" TODO: sort out the mess with the [try] command!
32 .SH EXAMPLE
33 .PP
34 Compute the factorial of a number.
35 .PP
36 .CS
37 proc factorial {n {accum 1}} {
38 if {$n < 2} {
39 return $accum
40 }
41 \fBtailcall\fR factorial [expr {$n - 1}] [expr {$accum * $n}]
42 }
43 .CE
44 .PP
45 Print the elements of a list with alternating lines having different
46 indentations.
47 .PP
48 .CS
49 proc printList {theList} {
50 if {[llength $theList]} {
51 puts "> [lindex $theList 0]"
52 \fBtailcall\fR printList2 [lrange $theList 1 end]
53 }
54 }
55 proc printList2 {theList} {
56 if {[llength $theList]} {
57 puts "< [lindex $theList 0]"
58 \fBtailcall\fR printList [lrange $theList 1 end]
59 }
60 }
61 .CE
62 .SH "SEE ALSO"
63 apply(n), proc(n), uplevel(n)
64 .SH KEYWORDS
65 call, recursion, tail recursion
66 '\" Local Variables:
67 '\" mode: nroff
68 '\" fill-column: 78
69 '\" End: