<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Sun Feb 12 12:02:33 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='136'>
<header><title>Large List Initialisation</title><author address="mailto:simon.geard@ntlworld.com">Simon Geard</author><status type='project' state='final' tclversion="8.5" vote='after'>$Revision: 1.5 $</status><history></history><created day='25' month='may' year='2003' /></header>
<abstract>This TIP proposes the addition of a list initialisation command so that large lists can be easily and efficiently initialised.</abstract>
<body><section title="Rationale">
<para>With the advent of the <emph style="italic">lset</emph> 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 <emph style="italic">lset</emph> and <emph style="italic">lindex</emph> to manipulate it than using <emph style="italic">lappend</emph> 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:</para>
<enumerate><item.e index='1'><para><emph style="italic">lappend</emph> in a loop</para><verbatim><vline encoding='base64'>IHVuc2V0IC1ub2NvbXBsYWluIHM=</vline><vline encoding='base64'>IGZvciB7c2V0IGkgMH0geyRpIDwgMTAwMH0ge2luY3IgaX0ge2xhcHBlbmQgcyB7eH19</vline></verbatim></item.e><item.e index='2'><para><emph style="italic">split</emph>ting of a string</para><verbatim><vline encoding='base64'>IHNldCBzIFtzcGxpdCBbc3RyaW5nIHJlcGVhdCB4IDEwMDBdICIiXQ==</vline></verbatim></item.e><item.e index='3'><para>Direct construction of the string form</para><verbatim><vline encoding='base64'>IHNldCBzIHhbc3RyaW5nIHJlcGVhdCAiIHgiIDk5OV0=</vline></verbatim></item.e></enumerate>
<para>None of these is particularly satisfactory. (1) seems inefficient since there are 1000 lappend operations, (2) is not general enough since it doesn&apos;t generalise to more than one character and (3) doesn&apos;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 &quot;everything is a string&quot; in Tcl.</para>
</section>
<section title="Implementation">
<para>I propose the introduction of a new command, <emph style="italic">lrepeat</emph>:</para>
<verbatim><vline encoding='base64'>IGxyZXBlYXQgPG51bWJlcj4gPGVsZW1lbnQxPiA/PGVsZW1lbnQyPj8gPzxlbGVtZW50Mz4/IC4uLg==</vline></verbatim>
<para>which returns a list of length &lt;number&gt; * (number of elements). The new list is the given element sequence repeated &lt;number&gt; times. &lt;number&gt; must be a positive integer and each &lt;element.&gt; a list or string.</para>
<para>Examples:</para>
<verbatim><vline encoding='base64'>IGxyZXBlYXQgMTAwIDAgICAgICAgICAgICAgICAgIC0gcmV0dXJucyBhIGxpc3Qgb2YgMTAwIHplcm9z</vline><vline encoding='base64'>IGxyZXBlYXQgMTAwIFtscmVwZWF0IDEwMCAwXSAgIC0gcmV0dXJucyBhIDEwMHgxMDAgbWF0cml4IChsaXN0IG9mIGxpc3RzKSBvZiB6ZXJvcw==</vline><vline encoding='base64'>IGxyZXBlYXQgMyBhIGIgYyAgICAgICAgICAgICAgIC0gcmV0dXJucyBhIG5pbmUtZWxlbWVudCBsaXN0IHthIGIgYyBhIGIgYyBhIGIgY30=</vline><vline encoding='base64'>IGxyZXBlYXQgMSBhIGIgYyAgICAgICAgICAgICAgIC0gaWRlbnRpY2FsIHRvIFtsaXN0IGEgYiBjXQ==</vline></verbatim>
</section>
<section title="Reference Implementation">
<para>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 <url ref="http://homepage.ntlworld.com/whiteowl/tcl/tcl843-lrepeat.patch"/> The directory also contains timing information and code which demonstrates a performance gain of over ten times for large lists.</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>

