Version 6 of lrepeat

Updated 2004-02-04 02:58:53

Eventually, the doc will be here: http://www.purl.org/tcl/home/man/tcl8.5/TclCmd/lsearch.html

 lrepeat number element1 ?element2 element3 ...?

This command builds up a list by repeating elements. It will be a part of Tcl 8.5.0 . Note that Tcl 8.5 is available in an early developer form from ftp://ftp.tcl.tk/pub/tcl/nightly-cvs/tcl-*gz . Early access to Tk 8.5 is there as well.

An example of how it will work is

  lrepeat 3 a

 a a a

RS: Note the possible confusion with string repeat's syntax:

 % string repeat a 5
 aaaaa

FW: The most minimalist way to implement this in straight Tcl for forwards-compatibility would in fact be to use string repeat:

 proc lrepeat {number args} {string repeat "$args " $number}

Or, for a more efficient solution:

 proc lrepeat {number args} {
   set res [list]
   for {set i 0} {$i < $number} {incr i} {
     concat res $args
   }
 }

Category Command