Thread overview
Slicing, not as modern as you thought
Jul 07, 2019
Patrick Schluter
Jul 07, 2019
watcher
Jul 07, 2019
Jonathan M Davis
Jul 08, 2019
Walter Bright
Jul 08, 2019
Era Scarecrow
Jul 08, 2019
Patrick Schluter
Jul 08, 2019
Walter Bright
July 07, 2019
After watching this retro-computing video [1] I was quite surprised to discover that the lowly Sinclair ZX81 already implemented slicing in its Basic string handling. The syntax was of course typical Basic ad hoc, but it seem to work quite nicely.

X$(1) would get the first character of string X$
X$(2 TO 4) would give the substr from offset 2 to 4
the interesting part is that it works also as lvalue so you could overwrite part of strings that way
X$(2 TO 4) = "ab"
etc.
Nice. I would have loved that at that time but neither TI-(Extended)-Basic nor Microsoft derived Basics (Applesoft) and later basics (GFA, Quick etc.) had this feature.

So thank you Walter Bright for reinventing something that was unfortunately lost in time.

[1]: https://www.youtube.com/watch?v=_d2g5BXdyfU&t=1811s
July 07, 2019
On Sunday, 7 July 2019 at 14:12:05 UTC, Patrick Schluter wrote:
> After watching this retro-computing video [1] I was quite surprised to discover that the lowly Sinclair ZX81 already implemented slicing in its Basic string handling. The syntax was of course typical Basic ad hoc, but it seem to work quite nicely.
>
> [...]

good old modula had that too.
July 07, 2019
On Sunday, July 7, 2019 8:12:05 AM MDT Patrick Schluter via Digitalmars-d wrote:
> After watching this retro-computing video [1] I was quite surprised to discover that the lowly Sinclair ZX81 already implemented slicing in its Basic string handling. The syntax was of course typical Basic ad hoc, but it seem to work quite nicely.
>
> X$(1) would get the first character of string X$
> X$(2 TO 4) would give the substr from offset 2 to 4
> the interesting part is that it works also as lvalue so you could
> overwrite part of strings that way
> X$(2 TO 4) = "ab"
> etc.
> Nice. I would have loved that at that time but neither
> TI-(Extended)-Basic nor Microsoft derived Basics (Applesoft) and
> later basics (GFA, Quick etc.) had this feature.
>
> So thank you Walter Bright for reinventing something that was unfortunately lost in time.
>
> [1]: https://www.youtube.com/watch?v=_d2g5BXdyfU&t=1811s

I don't think that Walter has ever claimed to invent it. IIRC, he got it from Pascal, though https://en.wikipedia.org/wiki/Array_slicing doesn't list Pascal as one of the languages with array slicing (personally, I haven't used Pascal enough to recall whether it has it or not). It does list perl and python though, and IIRC, some of the stuff in D was inspired by those languages. A lot of things in D actually come from other languages having them and Walter incorporating them or someone else proposing them and having them incorporated into D. Even ranges don't originate with D (Andrei got the idea from someone else), though I'm not aware of any other languages that used them like we do prior to D. Some stuff was invented for D, but most of it ended up in D by trying to take the best of ideas from elsewhere. I'm sure that plenty of those ideas are new to people coming to D though, since a lot of programmers have only used a few languages.

- Jonathan M Davis



July 08, 2019
On Sunday, 7 July 2019 at 14:12:05 UTC, Patrick Schluter wrote:
> the lowly Sinclair ZX81 already implemented slicing in its Basic string handling.

 So present in BASIC which Microsoft did.

 Keep in mind all 6502's had super limited memory, an 8bit chip, 8bit address space & registers with *SOME* 16 bit addressing options (including using the 0-page, or addresses hard coded).

 So with super limited space and no real memory management (you can sorta allocate space but never free it); That and strings 99% of the time were being replaced (or printed) and not modified in place, it's easy enough to reason they'd rather point to the same address (and not slow it down with duplicating data or have to deal with running out of precious 20k of ram). Arrays in BASIC were as you said, have an offset an a size. But there's no real dup option or the like, or splitting appending or doing interesting and fun things with the GC.

 Interesting to bring up. Honestly tinkering with Burrow Wheeling Transformation, i did something similar, doing a single short array to slice and make a huge number of arrays, but keeping allocation to a minimum (more for performance reasons and not for minimal memory usage).

 Also, you could have easily done the same thing in C anyways, as it's all just pointers and ints anyways, nothing would be stopping you from pointing to the middle of a string and then just saying it's N length (unless you absolutely required the \0 terminating byte).
July 07, 2019
On 7/7/2019 2:34 PM, Jonathan M Davis wrote:
> I don't think that Walter has ever claimed to invent it. IIRC, he got it
> from Pascal,

It's not from Pascal. It's from discussions I had with Jan Knepper about arrays. I'm not sure if it was his idea or mine. Doesn't really matter, it's a great idea.
July 08, 2019
On Monday, 8 July 2019 at 04:39:10 UTC, Era Scarecrow wrote:
> On Sunday, 7 July 2019 at 14:12:05 UTC, Patrick Schluter wrote:
>> the lowly Sinclair ZX81 already implemented slicing in its Basic string handling.
>
>  So present in BASIC which Microsoft did.

No, Microsoft BASIC did not have slicing. String handling in it was dynamic and garbage collected. Very few string functions would work inplace and it was easy to get GC pauses because any string operation would allocate new strings all the time: LEFT$, RIGHT$, MID$, STR$, RTRIM$, LTRIM$ all would allocate a new string.
Most other Basics followed scheme and believe me, there were a lot of BASIC dialects in the 8 bit era (TI-Basic, TI-Extended-Basic, Sharp Pocket Computer Basic, BBC-Basic, HP-Basic, all quite different in syntax and features but all more of less following the string handling of MS-Basic). That's why I was quite surprised when understanding Sinclair's Basic slice syntax. That explains also why, I never was able as a kit, to adapt listings in magazines of Sinclair games to my TI-99/4A or to my Apple II. A thing that was much easier with other micros of that time.


>
>  Keep in mind all 6502's had super limited memory, an 8bit chip, 8bit address space & registers with *SOME* 16 bit addressing options (including using the 0-page, or addresses hard coded).

Sinclair were Z80 but granted, it was not that much better than 6502.

>
>  So with super limited space and no real memory management (you can sorta allocate space but never free it); That and strings 99% of the time were being replaced (or printed) and not modified in place, it's easy enough to reason they'd rather point to the same address (and not slow it down with duplicating data or have to deal with running out of precious 20k of ram). Arrays in BASIC were as you said, have an offset an a size. But there's no real dup option or the like, or splitting appending or doing interesting and fun things with the GC.

No, string handling in the Basics of the 8 bit micros were all fully GC.

>
>  Interesting to bring up. Honestly tinkering with Burrow Wheeling Transformation, i did something similar, doing a single short array to slice and make a huge number of arrays, but keeping allocation to a minimum (more for performance reasons and not for minimal memory usage).
>
>  Also, you could have easily done the same thing in C anyways, as it's all just pointers and ints anyways, nothing would be stopping you from pointing to the middle of a string and then just saying it's N length (unless you absolutely required the \0 terminating byte).

The game changing detail in D is that [] is a fat pointer supported by the language. So no heroic efforts to bind the pointer with the length as is require in C.



July 08, 2019
On 7/8/2019 12:32 AM, Patrick Schluter wrote:
> The game changing detail in D is that [] is a fat pointer supported by the language. So no heroic efforts to bind the pointer with the length as is require in C.

Yup. A simple detail makes all the difference. It enables array bounds checking, too :-)

https://www.digitalmars.com/articles/b44.html


July 09, 2019
On Sunday, 7 July 2019 at 14:12:05 UTC, Patrick Schluter wrote:
> After watching this retro-computing video [1] I was quite surprised to discover that the lowly Sinclair ZX81 already implemented slicing in its Basic string handling. The syntax was of course typical Basic ad hoc, but it seem to work quite nicely.

High level languages often have substring "slices" of some kind, though. BASIC dialects borrowed a lot from "real" languages.

I think Simula got its string handling standardised  in 1968.  I think Simula had both value and reference semantics for strings. Assignment-operator ":=" copies by value and ":-" sets a reference. The standard calls slices "SectionText Frames":

http://simula67.at.ifi.uio.no/Standard-86/chap_2.htm