lseq(n)

๐Ÿ“ฆ
version 9.0.3 ยท 2 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05

๐ŸŽ€ lseq(n) โ€” bestie, let's build a whole numeric sequence ๐ŸŽ€

ok so this is the manual page for lseq, a Tcl Built-In Command, and honestly? it's giving "make me a list of numbers real quick" and I am obsessed. no cap, it does exactly what it says on the tin and the tin is cute.


โœจ NAME โœจ

lseq โ€” Build a numeric sequence returned as a list ๐Ÿฆ‹


๐ŸŒธ SYNOPSIS ๐ŸŒธ

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

likeโ€ฆ look at those forms. three whole moods. pick ur fighter ๐Ÿ’…


๐Ÿ’– DESCRIPTION ๐Ÿ’–

The lseq command creates a sequence of numeric values โ€” and those values can be either wide integers or doubles โ€” using the parameters you hand it: start, end, and step. The operation argument .. or to is what defines the range. The count option is used to define a count of the number of elements in the list. And there's a short form too: if you just give it a single count value, it'll create a range from 0 to count-1. iconic behavior honestly โœจ

lseq can produce both increasing and decreasing sequences, bestie. When both start and end are provided without a 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 an empty list is returned. like, nothing. ghosted. ๐Ÿ’€ for example:

              % lseq 1 to 5    ;# increasing
              โ†’ 1 2 3 4 5

              % lseq 5 to 1    ;# decreasing
              โ†’ 5 4 3 2 1

              % lseq 0 0.5 by 0.1  ;# doubles
              โ†’ 0.0 0.1 0.2 0.3 0.4 0.5

              % 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 โ€” start, end, step, and count โ€” may also be a valid expression. The expression will be evaluated and the numeric result will be used. But heads up: an expression that does not evaluate to a number will produce an invalid argument error. so keep it numeric, babe ๐Ÿ™ˆ

Start defines the initial value, and end defines the limit โ€” not necessarily the last value, ok? important distinction, don't come for me. lseq produces a list with count elements, and if count is not supplied, it is computed as:

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

math girlies rise ๐Ÿงฎ๐Ÿ’œ


๐ŸŒˆ EXAMPLES ๐ŸŒˆ

ok this is the fun part. watch:

              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

the range check one? main character energy. and that last one literally squares everything, ate and left no crumbs ๐Ÿ”ฅ


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

run don't walk to these besties:


๐Ÿ’… KEYWORDS ๐Ÿ’…

element, index, list โ€” the holy trinity fr fr ๐ŸŒธ


ok that's the whole page, no skips, no cap. go make some sequences, bestie ๐Ÿ’–

and that's a wrap!! this was the lseq(n) page from Tcl 9.0 ๐Ÿซถ mwah, tysm for reading, go forth and sequence responsibly โœจ๐Ÿ‘‘

๐Ÿ“ฆ where this page comes from