nvec ← from ##.to unto                      ⍝ Sequence ⍺ .. ⍵

Returns a vector of numbers ⍺ to ⍵ inclusive. ⍺ is the _first_ value in the seq-
uence, and ⍵ the _last_. More generally, ⍺ may be a 2-item vector, which determ-
ines the first two items of the sequence and hence the _step_ size.

Notice  that  any of _first_, _step_ and _last_ may be positive, negative and/or
fractional.  If  _last_  is  not  a  whole  number  of _step_s from _first_, the
sequence will stop short of _last_.

Technical notes:

The following simple function suffices for the ascending sequence ⍺, ⍺+1, ··· ⍵.
Notice how (·-⎕io) makes it independent of index origin:

    to←{(⍺-⎕io)+⍳1+⍵-⍺}

Useful  refinements  to  the  function allow _descending_ sequences and optional
_step_ sizes other than 1:

    to←{⎕io←0                                   ⍝ Sequence ⍺ .. ⍵
        from step←1 ¯1×-\2↑⍺,⍺+×⍵-⍺             ⍝ step default is +/- 1.
        from+step×⍳0⌈1+⌊(⍵-from)÷step+step=0    ⍝ ⍺ thru ⍵ inclusive.
    }

Attempts to re-cast the function as a one-liner, include:

    to←{From+0,+\Step⍴⍨0⌈⌊Inc÷Step←--/2⍴⍺,From+×Inc←⍵-From←1⍴⍺}

    to←{↑+/⍵{⍵×{⍵-⎕IO}⍳1+0⌈⌊(⍺⍺-⍺)÷⍵+⍵=0}\1 ¯1×-\2↑⍺,⍺+×⍵-⍺}

    to←{(⍬⍴⍺)+(|-/⌽2↑⍺,⍺+1){⍺×(×⍵)×(-⎕IO)+⍳1+⌊|⍵÷⍺}⍵-⍬⍴⍺}

VMJ  suggests the following extension, which accepts an optional explicit _step_
size  as  a second item of its _right_ argument. _step_ defers to _next_ if both
are supplied.

    to←{
        ⎕IO←0 ⋄ ⍺←1 ⋄ z←0.0000000001
        (a n)←2↑⍺,⍺ ⋄ (b s)←2↑⍵,1
        d←|s{⍵≠0:⍵ ⋄ ⍺≠0:⍺ ⋄ 1}n-a
        {⍵×z<|0-⍵}a+d{(⍺××⍵)×⍳1+⌊|⍵÷⍺}b-a
    }

    1 3 to 5      ⍝ using: _next_
1 3 5

    1 to 5 2      ⍝ using: _step_
1 3 5

Bob Smith suggests this extension for nested sequences:

    to←{                                    ⍝ Sequence ⍺ .. ⍵
        from step←⊂¨1 ¯1×-\2↑⍺,⍺+×⍵-⍺       ⍝ step default is +/- 1.
        size←0⌈1+⌊⊃(⍵-from)÷step+step=0     ⍝ shape of result
        from+step×(⍳size)-⎕io               ⍝ ⍺ thru ⍵ inclusive.
    }

    1 disp (¯1 6)(0 4) to ⊂3 ¯2
┌→───┬────┬────┬────┬─────┐
↓¯1 6│¯1 4│¯1 2│¯1 0│¯1 ¯2│
├~──→┼~──→┼~──→┼~──→┼~───→┤
│0 6 │0 4 │0 2 │0 0 │0 ¯2 │
├~──→┼~──→┼~──→┼~──→┼~───→┤
│1 6 │1 4 │1 2 │1 0 │1 ¯2 │
├~──→┼~──→┼~──→┼~──→┼~───→┤
│2 6 │2 4 │2 2 │2 0 │2 ¯2 │
├~──→┼~──→┼~──→┼~──→┼~───→┤
│3 6 │3 4 │3 2 │3 0 │3 ¯2 │
└~──→┴~──→┴~──→┴~──→┴~───→┘

We could extend Bob's idea to accommodate character and complex arguments:

    to←{                                    ⍝ Sequence ⍺ .. ⍵
        type←10|⎕DR ⍺,⍵                     ⍝ item type.
        type∊0 2:⎕UCS(⎕UCS ⍺)∇ ⎕UCS ⍵       ⍝ char sequences.
        type=9:⍺ ∇{                         ⍝ complex arg
            j←1 0J1∘(+.×)                   ⍝ number from (re im) pair
            p←9 11∘○¨                       ⍝ (re im) pair from number
            j¨(p ⍺)⍺⍺ p ⍵                   ⍝ complex sequence via nested pairs.
        }⍵                                  ⍝
        from step←⊂¨1 ¯1×-\2↑⍺,⍺+×⍵-⍺       ⍝ step default is +/- 1.
        size←0⌈1+⌊⊃(⍵-from)÷step+step=0     ⍝ shape of result
        from+step×(⍳size)-⎕IO               ⍝ ⍺ thru ⍵ inclusive.
    }

    'a' to 'z'
abcdefghijklmnopqrstuvwxyz

    'AC' to 'Z'
ACEGIKMOQSUWY

    0 to 3j4
0 0J1 0J2 0J3 0J4
1 1J1 1J2 1J3 1J4
2 2J1 2J2 2J3 2J4
3 3J1 3J2 3J3 3J4

Adám Brudzewsky suggests the following version. Adám says:

Fills in the sequence gap between the right end of ⍺ and  the  left  end  of  ⍵.
Depends on the data type of the first element of ⍵, and on the  number of trail-
ing elements in ⍺ with the same data type.  For a numeric first element of ⍵ and
with two or more trailing numbers in ⍺, it extrapolates from the last two  elem-
ents of ⍺, letting their difference determine  the  step  size.  With  one  (for
character first elements of ⍵: or more) trailing compatible element(s) in ⍺,  it
fills the gap by using a step size of ±1 from the last element  of  ⍺.  If there
are no trailing compatible elements in ⍺ or if called monadically, it begins the
sequence at 0, using a step size of ±1.

    to←{⎕IO←0                                             ⍝ Fill sequence gap
        Char←0 2∊⍨10|⎕DR                                  ⍝ character?
        end←⊃⍵                                            ⍝ of sub-sequence
        tail←1↓⍵                                          ⍝ to be appended
        charend←Char end                                  ⍝ character ⍵?
        default←⎕UCS⍣charend⊢0                            ⍝ default begin from 0
        ⍺←default                                         ⍝ default if monadic
        charbegins←Char¨¯2↑⍺                              ⍝ character ⍺?
        lead←-(2-charend)⌊(≢⍺)⌊+/charend=charbegins       ⍝ to be considered
        head←lead↓⍺                                       ⍝ to be prepended
        begin←(¯1⌊lead)↑¯2↑default,lead↑⍺                 ⍝ first one/two items
        charend:head,tail,⍨⎕UCS(⎕UCS begin)∇ ⎕UCS end     ⍝ char sequences
        from step←-⍨\2↑begin,begin+×end-begin             ⍝ step default is +/- 1
        head,tail,⍨from+step×⍳0⌈1+⌊(end-from)÷step+step=0 ⍝ ⍺ thru ⍵ inclusive
    }

    'a' to 'zA' to 'Z'              ⍝ English alphabet
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

    'a'to'zæøåA'to'ZÆØÅ'            ⍝ Danish alphabet
abcdefghijklmnopqrstuvwxyzæøåABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ

    to 10 20 to 100 200 300 400     ⍝ Numbers
0 1 2 3 4 5 6 7 8 9 10 20 30 40 50 60 70 80 90 100 200 300 400

    0 ¯1J2 to ¯4J8                  ⍝ Complex numbers
0 ¯1J2 ¯2J4 ¯3J6 ¯4J8

    '='¯2 to 3 9 8.5 to 6,'+-×÷À' to 'Å' to 3       ⍝ Mixed data types
= ¯2 ¯1 0 1 2 3 9 8.5 8 7.5 7 6.5 6 +-×÷ÀÁÂÃÄÅ 0 1 2 3

Examples:

      3 to 10               ⍝ Inclusive ascending sequence.
3 4 5 6 7 8 9 10

      10 to 3               ⍝ Descending sequence.
10 9 8 7 6 5 4 3

      7 to 7                ⍝ Single-item sequence.
7
      5 7 to 13             ⍝ 2-item start determines step.
5 7 9 11 13

      ¯10 ¯15 to ¯25        ⍝ Negative start and step.
¯10 ¯15 ¯20 ¯25

      1.5 1.7 to 2.5        ⍝ Fractional start and step.
1.5 1.7 1.9 2.1 2.3 2.5

      0 3 to 10             ⍝ Sequence may stop short of _last_.
0 3 6 9

See also: sieve

Back to: contents

Back to: Workspaces