Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

lreplace command in tcl

1 view
Skip to first unread message

Phil Soltan

unread,
May 21, 1996, 3:00:00 AM5/21/96
to

I'm having a hard time seeing the consistency in a Tcl command and I was
wondering if it was a bug or a feature. The 'lappend' command not only
returns the appended list but modifies the variable being appended also.
The 'lreplace' command on the other hand returns the string with the
desired portion replaced but doesn't actually modify the variable. For
example:

tcl>lappend test X Y Z
X Y Z
tcl>set test
X Y Z
tcl>lreplace $test 0 0
Y Z
tcl>set test
X Y Z

"Tcl and the Tk Toolkit" book makes it seem like 'lreplace' should
actually change the variable. Does anyone care to defend/attack this
aspect of Tcl?

Phil Soltan
OAO
Point Mugu, CA

Eric Vought

unread,
May 21, 1996, 3:00:00 AM5/21/96
to

<snip>

> "Tcl and the Tk Toolkit" book makes it seem like 'lreplace' should
> actually change the variable. Does anyone care to defend/attack this
> aspect of Tcl?

The routine that is comparable to lreplace for adding values to a list
is linsert:

linsert $foo end gobbledegook

This will add the element "gobbledegook" to the end of the contents of
the variable foo. foo will not eb modified by this operation.

lappend is made avaliable for performance reasons.
Because lappend operates directly on the variable, it does not have to
be copied and substitution doesn't have to be performed. Additionally,
the contents of the variable do not have to be parsed (to find list
element boundaries) when append is used, and the they do when linsert is
used. This makes lappend considerably faster when adding elements
repeatedly to long lists, but it lacks the flexibility of linsert in
that you can only use it to add elements to the end, not to arbitrary
locations in the list.

Mark Hartswood

unread,
May 28, 1996, 3:00:00 AM5/28/96
to

In article <Drrvr...@avalon.chinalake.navy.mil>, Phil Soltan <pso...@echo1.mugu.navy.mil> writes:
> I'm having a hard time seeing the consistency in a Tcl command and I was
> wondering if it was a bug or a feature. The 'lappend' command not only
> returns the appended list but modifies the variable being appended also.
> The 'lreplace' command on the other hand returns the string with the
> desired portion replaced but doesn't actually modify the variable. For
> example:
>
> tcl>lappend test X Y Z
> X Y Z
> tcl>set test
> X Y Z
> tcl>lreplace $test 0 0
> Y Z
> tcl>set test
> X Y Z
>
> "Tcl and the Tk Toolkit" book makes it seem like 'lreplace' should
> actually change the variable. Does anyone care to defend/attack this
> aspect of Tcl?

I suspect that the behaviour of lappend is due to an informed design
decision. Consistency as a general guideline is useful for reducing the
learning curve for a given language, however, if one is dogmatic it is
possible that a language will emerge which is easy to learn but cumbersome
to use. Exceptions to a given paradigm enable greater power in the context
of frequently occouring task scenarios (at the expense of initial comprehension).

The case in question enables the tcl idiom:

for {set i 0} {$i<20} {incr i} {

lappend foo bar
}

which is typical lappend usage. Compare this with:

set foo ""

for {set i 0} {$i<20} {incr i} {

set foo [lappend $foo bar]
}

which much more cumbersome if your going to be doing this sort
of thing often.

It could be argued that other list commands are more
likely to be used in a context which preserve the original list
in its unmodified form. However - in the spirit of tcl it is
entirely possible to generate wrapper functions to make list
commands work in any way you want...

(Let me know if this is wildly off the mark)

Cheers,
Mark

0 new messages