String and List Reversal OperationsDonal K. Fellows$Revision: 1.4 $Tcl lreverse
This TIP proposes adding commands to reverse the order of characters in strings and elements in lists.
According to a recent thread on [], there are a number of use cases for reversing strings and lists. While it has always been possible to write Tcl code to do this, it has typically been a fairly inefficient operation, and for some algorithms (admittedly including ones that are benchmarked when comparing Tcl to other languages) this can be the source of a painful slowdown. By putting an efficient implementation in the Tcl core, we will speed up quite a bit of code, more than I originally anticipated it seems; this will make us look better to programmers without much experience with the language too.
I propose to add two commands, lreverse to reverse the order of items in a list, and string reverse (a subcommand of string) to reverse the order of characters in a string. I do not propose to provide any C-level API for performing these operations.
This shall have the following syntax:
lreverse list
It shall return a list that is the same as the input list but with the elements in reverse order.
This shall have the following syntax:
string reverse string
It shall return a string that is the same as the input string but with the characters in reverse order. (Note that it shall also return a byte array when the input is a byte array.)
This document has been placed in the public domain.