TIP 331: Allow [lset] to Extend Lists

Author:		Kevin B. Kenny <[email protected]>
State:		Final
Type:		Project
Vote:		Done
Created:	22-Sep-2008
Tcl-Version:	8.6


This TIP proposes to modify the lset command to allow it to extend lists.


The lset command shall be modified to allow the index end+1 (or any equivalent index designating the element one beyond the last element of the list). For the simple usage:

    lset list end+1 foo

the effect is the same as if the script had evaluated the command:

    lappend list foo

For the usage:

    lset list $n end+1 foo

the effect is much the same as if the script had evaluated:

    set temp [lindex $list $n]
    lappend temp foo
    lset list $n $temp

(except, of course that no temp variable is created)

The usage:

    lset list end+1 0 foo

or, equivalently,

    lset list end+1 end+1 foo

is equivalent to

    lappend list [list foo]


Clearly, there are equivalent constructs to all of these usages. Nevertheless, it is convenient not to have to worry about whether a given index is in bounds, particularly when iterating through a vector or matrix. With the proposed change, many of the standard algorithms in linear algebra will just work, without either having to preinitialize a list to a given size or to have a test to determine whether to use lappend or lset to store a given element.


Explicitly out of scope is the treatment of indices greater than or equal to end+2. Setting non-contiguous elements would raise false expectations of sparse lists, NULL elements, and so on.


Copyright © 2008 by Kevin B. Kenny.

This document may be distributed subject to the terms and conditions set forth in the Open Publication License, version 1.0 http://www.opencontent.org/openpub/ .