Version 15 of lreplace

Updated 2005-09-15 13:30:01 by escargo

Replace elements in a list with new elements

The documentation for this command can be found at http://www.purl.org/tcl/home/man/tcl8.5/TclCmd/lreplace.htm

 lreplace list first last ?element element ...?  

Lreplace returns a new list formed by replacing one or more elements of list with the element arguments. First gives the index in list of the first element to be replaced (0 refers to the first element). If first is less than zero then it refers to the first element of list; the element indicated by first must exist in the list. Last gives the index in list of the last element to be replaced. If last is less than first then no elements are deleted; the new elements are simply inserted before first. First or last may be end (or any abbreviation of it) to refer to the last element of the list.

The element arguments specify zero or more new arguments to be added to the list in place of those that were deleted. Each element argument will become a separate element of the list. If no element arguments are specified, then the elements between first and last are simply deleted. (from: TclHelp)


Examples:

 % lreplace  {a b c} 0 0 @
 @ b c
 % lreplace  {a b c} 1 1 
 a c

If the last argument of lreplace was made optional, one could essentially just alias ldelete to lreplace CmCc


TFW Feb 20, 2004 - Inplace replacements

Often time we have a long list we want to replace "in-place", that is without copying the list to a new altered list. Using the K combinator it is quite easy. (Note 8.5 is needed for the {expand} syntax)

 proc lipreplace {_list first last args} {
    upvar $_list list
    set list [lreplace [K $list [set list ""]] $first $last {expand}$args]
 }
 > set A [list 1 2 3]
 1 2 3
 > lipreplace A 1 end c d
 1 c d

escargo 15 Sep 2005 - Perhaps there is a better place for the following discussion, but this is where I saw a specific feature mentioned that I want to raise an issue about.

List commands bounds checking and associated behavior

The description above states (in part): "If first is less than zero then it refers to the first element of list."

I see two problems with this:

  • What should the behavior be when indexing outside the bounds of a list?
  • How does this behavior vary from other list operations?

Personally, I see allowing negative indexes (first or last) to be errors, and ones that are silent failures at that. (This is unlike Icon where at least some negative indexes have semantically valid meaning.) Perhaps list operations that do indexing should have a -strict option so that if indexes are outside the proper ranges, errors would be thrown.

On the second issue, lindex allows negative indexes (and indexes greater than the length of the list), but returns no values. The negative index is not coerced to zero. Thus the same index value (a negative one) refers to no element with lindex but the zeroth element with lreplace. Perhaps an item for the Tcl 9.0 WishList would be consistent behavior for list and string indexing semantics.


See also: linsert - lappend - list


Category Command