poke-devel
[Top][All Lists]

## Proposal to change the ranges in array trimming to be "half-open" interv

 From: Mohammad-Reza Nabipoor Subject: Proposal to change the ranges in array trimming to be "half-open" interval Date: Thu, 29 Oct 2020 19:53:30 +0330

```Hi.

Let's talk about ranges in array trimming!

```poke
defvar a = [1, 2, 3, 4, 5];
defvar b = a[1:3];

assert (b == [2, 3, 4]);
```

The `1:3` range is an inclusive interval from `1` to `3`.
Mathematically, the interval is `[1, 3]`. A "closed interval".

IMHO, closed intervals are hard to deal with.

Let me explain a little more.

This is the scheme used by Poke for indexing elements:

1, 2, 3, 4, 5
^  ^  ^  ^  ^
|  |  |  |  |
0  1  2  3  4

But I argue that the following scheme for indexing elements is much more
powerful and easier to deal with:

1, 2, 3, 4, 5
^ ^  ^  ^  ^  ^
| |  |  |  |  |
0 1  2  3  4  5

For a sequence of `N` elements there are `N+1` positions.
Positions points **between** elements not **at** elements.

This is the approached used in `STL` of `C++` (which is designed by Alex
Stepanov).

The ranges in STL are half-open intervals (mathematically denoted as `[f, l)`).
- The number of elements in a range is `l - f`.
- Dividing a range into two sub-ranges are simple:
`[f, m)` and `[m, l)`

New Poke:

```poke'
assert (a[0:1] == );
assert (a[0:5] == [1, 2, 3, 4, 5]);
/* a[0:0] is an empty array */
assert (a[:2] + a[2:] == a);  /* instead of `a[:2] + a[2+1:]` */
```

Regards,