lseq(3tcl)

πŸ“¦
version 9.0.4 Β· 3 versions
, in ubuntu stonking

πŸ’– lseq β€” bestie, it's the Tcl Built-In Commands page, 3tcl section, and we're building sequences πŸ’–

✨ NAME ✨

lseq β€” build a numeric sequence and get it back as a list πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

lseq start ?(..|to)? end ??by? step?
lseq start count count ??by? step?
lseq count ?by step?

🌸 DESCRIPTION 🌸

okay so the lseq command makes a sequence of numeric values β€” and those values can be wide integers or doubles, your call β€” using the params start, end, and step. The operation argument .. or to is what defines the range, like it's the little official handshake for "yeah this is a range". The count option is a whole different thing: it defines a count of the number of elements in the list. And there's a short form, where you just hand it a single count value, and boom: a range from 0 to count-1. 🫢

lseq can do both increasing and decreasing sequences, she's versatile like that. When both start and end show up with no step value, then if start <= end, the sequence will be increasing, and if start > end it will be decreasing. But if a step value is included, its sign should agree with the direction of the sequence (descending β†’ negative, ascending β†’ positive), otherwise the list will have a length of 0. like, an empty list, a little nothing-burger. For example:

# increasing:
% lseq 1 to 5
β†’ 1 2 3 4 5

# decreasing:
% lseq 5 to 1
β†’ 5 4 3 2 1

# doubles:
% lseq 0 0.5 by 0.1
β†’ 0.0 0.1 0.2 0.3 0.4 0.5

# decreasing, step with wrong sign:
% lseq 6 to 1 by 2
β†’ {}

# step of 0,
% lseq 3 to 9 by 0
β†’ 3

start defines the initial value and end defines the limit β€” not necessarily the last value, bestie, keep that in mind. lseq produces a list with count elements, always, even if the step value is 0. And if count is not supplied, it is computed as:

count = int( ( (end - start) / step ) + 1 )

πŸ’… 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

lseq 1 count 5 by 0
β†’ 1 1 1 1 1

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

πŸ‘‘ SEE ALSO πŸ‘‘

foreach(3tcl), list(3tcl), lappend(3tcl), lassign(3tcl), lindex(3tcl), linsert(3tcl), llength(3tcl), lmap(3tcl), lpop(3tcl), lrange(3tcl), lremove(3tcl), lreplace(3tcl), lreverse(3tcl), lsearch(3tcl), lset(3tcl), lsort(3tcl)

the whole list squad fr fr, go read all of them, they're iconic πŸ’œ


⭐ KEYWORDS ⭐

element, index, list


okay and that's the whole vibe check β€” this page is straight outta Tcl 9.0, lseq(3tcl), and it literally ate πŸ’…βœ¨ no cap, go build some sequences bestie, you got this πŸ¦‹πŸ’–πŸŒΈπŸŽ€

πŸ“¦ where this page comes from