TIP #272: String and List Reversal Operations

Title:String and List Reversal Operations
Version:$Revision: 1.4 $
Author:Donal K. Fellows <donal dot k dot fellows at manchester dot ac dot uk>
Created:Wednesday, 23 August 2006
Keywords: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 news:comp.lang.tcl [1], 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.

Proposed Change

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.

The lreverse Command

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.

The reverse Subcommand of the string Command

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.

Powered by Tcl[Index] [History] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows