Skip to content
Tcl Source Code Artifact [4245f43e0d]
Login

Artifact [4245f43e0d]

Artifact 4245f43e0dee513780840b5be195829ed4751a2a95fc5283ac5df9e7a8dec841:


'\"
'\" Copyright (c) 2022 Eric Taylor.  All rights reserved.
'\"
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
'\"
.TH lseq n 9.0 Tcl "Tcl Built-In Commands"
.so man.macros
.BS
'\" Note:  do not modify the .SH NAME line immediately below!
.SH NAME
lseq \- Build a numeric sequence returned as a list
.SH SYNOPSIS
.nf
\fBlseq \fIstart end\fR
\fBlseq \fIstart\fR ?\fBto\fR? \fIend\fR ?\fIstepSpec\fR?
\fBlseq \fIstart\fR ?\fB..\fR? \fIend\fR ?\fIstepSpec\fR?
\fBlseq \fIstart \fBcount\fI count\fR ?\fIstepSpec\fR?

\fBlseq \fIcount\fR ?\fBby \fIstepValue\fR?
.fi
.BE
.SH DESCRIPTION
.PP
The \fBlseq\fR command creates a sequence of numeric values, which may
be either wide integers or doubles, using the arguments
\fIstart\fR, \fIend\fR, \fIcount\fR, and optionally
\fIstepSpec\fR/\fIstepValue\fR. 
.PP
If a \fIstart\fR value is specified as the first argument, the \fIend\fR value
of the sequence can be specified as the next argument, optionally preceeded by
the literal word "\fBto\fR" or "\fB..\fR". Alternatively, the \fIcount\fR
(number of elements in the sequence) can be defined instead when preceeded by
the word \fBcount\fR. In both cases, the interval between the subsequent
numbers of the sequence can be specified using the optional \fIstepSpec\fR
argument. It is defined as "?\fBby\fR? \fIstepValue\fR", i.e. the optional word
\fBby\fR followed by the step value.
.PP
A short form use of the command with a single argument \fIcount\fR will create a
sequence from 0 to \fIcount\fR-1. It can optioanlly be followed by a step value
preceeded by the word \fBby\fR.
.PP
The \fBlseq\fR command can produce both increasing and decreasing
sequences. When both \fIstart\fR and \fIend\fR are provided without a
\fIstepValue\fR, then if \fIstart\fR <= \fIend\fR, the sequence will be
increasing and if \fIstart\fR > \fIend\fR it will be decreasing. If the
\fIstepValue\fR value is included, it's sign should agree with the direction of the
sequence (descending \(-> negative and ascending \(-> positive), otherwise the
list will have a length of 0.  For example:
.RS
.PP
.CS \"
# increasing:
% \fBlseq\fR 1 to 5
\fI\(-> 1 2 3 4 5\fR

# decreasing:
% \fBlseq\fR 5 to 1
\fI\(-> 5 4 3 2 1\fR

# doubles:
% \fBlseq\fR 0 0.5 by 0.1
\fI\(-> 0.0 0.1 0.2 0.3 0.4 0.5\fR

# decreasing, step with wrong sign:
% \fBlseq\fR 6 to 1 by 2
\fI\(-> {}\fR

# step of 0,
% \fBlseq\fR 3 to 9 by 0
\fI\(-> 3\fR
.\"
.CE
.RE
.PP
\fIStart\fR defines the initial value and \fIend\fR defines the limit, not
necessarily the last value. \fBlseq\fR produces a list with \fIcount\fR
elements, always, even if the step value is 0. and if \fIcount\fR is not
supplied, it is computed as:
.RS
.PP
.CS
\fIcount\fR = int( ( (\fIend\fR - \fIstart\fR) / \fIstepValueq\fR ) + 1 )
.CE
.RE
.SH EXAMPLES
.CS
.\"
\fBlseq\fR 3
\fI\(-> 0 1 2\fR

\fBlseq\fR 3 0
\fI\(-> 3 2 1 0\fR

\fBlseq\fR 10 .. 1 by -2
\fI\(-> 10 8 6 4 2\fR

set l [\fBlseq\fR 0 -5]
\fI\(-> 0 -1 -2 -3 -4 -5\fR

\fBlseq\fR 1 count 5 by 0
\fI\(-> 1 1 1 1 1\fR

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

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

set i 17
\fI\(-> 17\fR

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

set sqrs [lmap i [\fBlseq\fR 1 10] { expr {$i*$i} }]
\fI\(-> 1 4 9 16 25 36 49 64 81 100\fR
.\"
.CE
.SH "SEE ALSO"
foreach(n), list(n), lappend(n), lassign(n), lindex(n), linsert(n), llength(n),
lmap(n), lpop(n), lrange(n), lremove(n), lreplace(n),
lreverse(n), lsearch(n), lset(n), lsort(n)
.SH KEYWORDS
element, index, list
'\" Local Variables:
'\" mode: nroff
'\" fill-column: 78
'\" End: