TIP #136: LARGE LIST INITIALISATION ===================================== Version: $Revision: 1.5 $ Author: Simon Geard State: Final Type: Project Tcl-Version: 8.5 Vote: Done Created: Sunday, 25 May 2003 URL: https://tip.tcl-lang.org136.html Post-History: ------------------------------------------------------------------------- ABSTRACT ========== This TIP proposes the addition of a list initialisation command so that large lists can be easily and efficiently initialised. RATIONALE =========== With the advent of the /lset/ command in Tcl 8.4 it seems to me that we need a method of efficiently initialising large lists that can then be used as areas of preallocated memory. From a users point of view it can be much easier to preallocate say a 1000 element array and then use /lset/ and /lindex/ to manipulate it than using /lappend/ to build it up. Having posted the idea to the tcl-core mailing list various alternatives were suggested to create a list of 1000 x characters: 1. /lappend/ in a loop unset -nocomplain s for {set i 0} {$i < 1000} {incr i} {lappend s {x}} 2. /split/ting of a string set s [split [string repeat x 1000] ""] 3. Direct construction of the string form set s x[string repeat " x" 999] None of these is particularly satisfactory. (1) seems inefficient since there are 1000 lappend operations, (2) is not general enough since it doesn't generalise to more than one character and (3) doesn't actually create a list. (2) and (3) also suffer from the problem that they are not at all obvious to new users and do nothing to dispel the notion that "everything is a string" in Tcl. IMPLEMENTATION ================ I propose the introduction of a new command, /lrepeat/: lrepeat ?? ?? ... which returns a list of length * (number of elements). The new list is the given element sequence repeated times. must be a positive integer and each a list or string. Examples: lrepeat 100 0 - returns a list of 100 zeros lrepeat 100 [lrepeat 100 0] - returns a 100x100 matrix (list of lists) of zeros lrepeat 3 a b c - returns a nine-element list {a b c a b c a b c} lrepeat 1 a b c - identical to [list a b c] REFERENCE IMPLEMENTATION ========================== I have implemented this command. A patch against the Tcl 8.4.3 source, which implements the command and provides both tests and documentation, is available from The directory also contains timing information and code which demonstrates a performance gain of over ten times for large lists. COPYRIGHT =========== This document has been placed in the public domain. ------------------------------------------------------------------------- TIP AutoGenerator - written by Donal K. Fellows