Skip to content
Tcl Source Code Artifact [052400b04e]
Login

Artifact [052400b04e]

Artifact 052400b04e23fa4602a8de658a50f5b88a62ba99958cbb9f8501755ed9ebb827:



CommandName: lseq ManualSection: n Version: 9.0 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

lseq - Build a numeric sequence returned as a list

Synopsis

::: {.synopsis} ::: [lseq]{.cmd} [start]{.arg} [end]{.arg} [lseq]{.cmd} [start]{.arg} [to]{.optlit} [end]{.arg} [stepSpec]{.optarg} [lseq]{.cmd} [start]{.arg} [..]{.optlit} [end]{.arg} [stepSpec]{.optarg} [lseq]{.cmd} [start]{.arg} [count]{.lit} [count]{.arg} [stepSpec]{.optarg}

[lseq]{.cmd} [count]{.arg} [[by]{.lit} [stepValue]{.arg}]{.optarg} :::

Description

The fBlseqfR command creates a sequence of numeric values using the arguments fIstartfR, fIendfR, fIcountfR, and optionally fIstepSpecfR/fIstepValuefR.

If a fIstartfR value is specified as the first argument, the fIendfR value of the sequence can be specified as the next argument, optionally preceeded by the literal word "fBtofR" or "fB..fR". Alternatively, the fIcountfR (number of elements in the sequence) can be defined instead when preceeded by the word fBcountfR. In both cases, the interval between the subsequent numbers of the sequence can be specified using the optional fIstepSpecfR argument. It is defined as "?fBbyfR? fIstepValuefR", i.e. the optional word fBbyfR followed by the step value.

A short form use of the command with a single argument fIcountfR will create a sequence from 0 to fIcountfR-1. It can optioanlly be followed by a step value preceeded by the word fBbyfR.

The fBlseqfR command can produce both increasing and decreasing sequences. When both fIstartfR and fIendfR are provided without a fIstepValuefR, then if fIstartfR <= fIendfR, the sequence will be increasing and if fIstartfR > fIendfR it will be decreasing. If the fIstepValuefR is included, it's sign should agree with the direction of the sequence (descending (-> negative and ascending (-> positive), otherwise an empty list is returned. For example:

% lseq 1 to 5    ;# increasing
\(-> 1 2 3 4 5

% lseq 5 to 1    ;# decreasing
\(-> 5 4 3 2 1

% lseq 6 to 1 by 2   ;# decreasing, step wrong sign, empty list

% lseq 1 to 5 by 0   ;# all step sizes of 0 produce an empty list

The numeric arguments in fIstartfR, fIendfR, fIstepValuefR and fIcountfR may also be valid expressions. The expression will be evaluated and the numeric result will be used. An expression that does not evaluate to a number will produce an invalid argument error.

Start defines the initial value and fIendfR defines the limit, not necessarily the last value. fBlseqfR produces a list with fIcountfR elements, and if fIcountfR is not supplied, it is computed as:

count = int( (end - start + stepValue) / stepValueq )

Examples

lseq 3
\(-> 0 1 2

lseq 3 0
\(-> 3 2 1 0

lseq 10 .. 1 by -2
\(-> 10 8 6 4 2

set l [lseq 0 -5]
\(-> 0 -1 -2 -3 -4 -5

foreach i [lseq [llength $l]] {
    puts l($i)=[lindex $l $i]
}
\(-> l(0)=0
\(-> l(1)=-1
\(-> l(2)=-2
\(-> l(3)=-3
\(-> l(4)=-4
\(-> l(5)=-5

foreach i [lseq {[llength $l]-1} 0] {
    puts l($i)=[lindex $l $i]
}
\(-> l(5)=-5
\(-> l(4)=-4
\(-> l(3)=-3
\(-> l(2)=-2
\(-> l(1)=-1
\(-> l(0)=0

set i 17
         \(-> 17
if {$i in [lseq 0 50]} { # equivalent to: (0 <= $i && $i <= 50)
    puts "Ok"
} else {
    puts "outside :("
}
\(-> Ok

set sqrs [lmap i [lseq 1 10] { expr {$i*$i} }]
\(-> 1 4 9 16 25 36 49 64 81 100