poke-devel
[Top][All Lists]

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

 From: Jose E. Marchesi Subject: Re: Proposal to change the ranges in array trimming to be "half-open" interval Date: Thu, 29 Oct 2020 17:40:33 +0100 User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

```Hi Mohammad.

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

Well, in an array subscript a position actually refers to the element
following the referred position, right, not the position itself :)

Reminds me of the Emacs pointer.

But yeah, I like the proposed change; using semi-open intervals makes
more sense for zero-based arrays.

And now is the time to do changes like this :)
Other opinions?

>
> 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] == [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,