Go to main content

man pages section 1: User Commands

Exit Print View

Updated: Wednesday, February 10, 2021
 
 

tailcall (1t)

Name

tailcall - Replace the current procedure with another command

Synopsis

tailcall command ?arg ...?

Description

tailcall(1t)                 Tcl Built-In Commands                tailcall(1t)



______________________________________________________________________________

NAME
       tailcall - Replace the current procedure with another command

SYNOPSIS
       tailcall command ?arg ...?
______________________________________________________________________________

DESCRIPTION
       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 ...?]]

       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]
                  }
              }


ATTRIBUTES
       See attributes(7) for descriptions of the following attributes:


       +---------------+------------------+
       |ATTRIBUTE TYPE | ATTRIBUTE VALUE  |
       +---------------+------------------+
       |Availability   | runtime/tcl-8    |
       +---------------+------------------+
       |Stability      | Uncommitted      |
       +---------------+------------------+
SEE ALSO
       apply(n), proc(n), uplevel(n)

KEYWORDS
       call, recursion, tail recursion



NOTES
       This     software     was    built    from    source    available    at
       https://github.com/oracle/solaris-userland.   The  original   community
       source was downloaded from  http://prdownloads.sourceforge.net/tcl/tcl-
       core8.6.7-src.tar.gz

       Further information about this software can be found on the open source
       community website at https://www.tcl.tk/.



Tcl                                   8.6                         tailcall(1t)