Skip to content

tailcall

Replace the current procedure frame with a call to another command.

Syntax

tcl
tailcall command ?arg ...?

Parameters

  • command: The command to call
  • arg ...: Arguments to pass to the command

Description

tailcall replaces the current procedure's stack frame with a call to the specified command. This enables tail call optimization, preventing stack overflow in deeply recursive procedures.

The command behaves as if the current procedure returned and then the specified command was called. This is more efficient than a regular call followed by return.

Examples

Tail-recursive factorial

Output

Tail-recursive sum

Output

State machine with tailcall

Output

Delegation pattern

Output

See Also

  • proc - Define procedures
  • return - Return from procedure
  • apply - Anonymous functions