⍝ Sequence ⍺ .. ⍵:

    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

    1 3 to 4                ⍝ stops short if unto not whole number of steps.
1 3
    ¯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 stops short of <last>.
0 3 6 9

    7 8 to 7                ⍝ single-item sequence.
7
    7 8 to 6                ⍝ null sequence.

⍝       ¯4↑1.001 1.002 to 2   ⍝ Roger's example
⍝   1.997 1.998 1.999 2

⍝ Bob Smith's extension:

    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 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 │
└────┴────┴────┴────┴─────┘

    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.
    }

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

    'a' to 'z'
abcdefghijklmnopqrstuvwxyz

    'AC' to 'Z'
ACEGIKMOQSUWY

⍝ Adám's version:

    to←{⎕IO←0                                             ⍝ Sequence
        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

Back to: code

Back to: Workspaces