1. [Phix Tutorial] Basic Sequence Actions

Phix Billiard Ball Diagrams

Item

An "item" works like a variable, you can assign it a new value.

                     To Item                      ?                   
s = "○◍○○" s[2] = '●' "○●○○"
s = "○◍○○" s[2] = "●●" {'○', "●●", '○','○'}

Fill

Atom to slice, all items get the same value.

                     Atom Fill                    ?                   
s = "○◍○○" s[2..2] = '●' "○●○○"
s = "○◍◍◍" s[2..4] = '●' "○●●●"

New Items

A "gap" starts with nothing, you can not assign an atom to a gap, but you can assign any sequence to a gap.

                     To Any Gap                   ?                   
s = "○◍○" s[2..1] = '●' "○◍○"
s = "○◍○" s[2..1] = { '●' } "○●◍○"
s = "○◍○" s[2..1] = "●" "○●◍○"
s = "○◍○" s[2..1] = "●●" "○●●◍○"
                     To Head Gap                  ?                   
s = "◍○○○" s[1..0] = '●' "◍○○○"
s = "◍○○○" s[1..0] = "●" "●◍○○○"
s = "◍○○○" s[1..0] = "●●" "●●◍○○○"
                     To Tail Gap                  ?                   
s = "○○○◍" s[$+1..$] = '●' "○○○◍"
s = "○○○◍" s[$+1 .. $] = "●" "○○○◍●"
s = "○○○◍" s[$+1 .. $] = "●●" "○○○◍●●"
new topic     » topic index » view message » categorize

2. Re: [Phix Tutorial] Basic Sequence Actions

Concatenation

Concatenate ?
x = '○' x = '◍' & x & '●' "◍○●"
s = "○○○" s = '◍' & s & '●' "◍○○○●"
s = "○○○" s = "◍" & s &"●" "◍○○○●"
s = "○○○" s = "◍◍◍" & s & "●●●" "◍◍◍○○○●●●"

Remove

Get rid of an item or slice.

Remove ?
s = "○◍○○" s[2..2] = {} "○○○"
s = "○◍◍◍" s[2..$] = {} "○"

Replace

Replace a slice with something completely new.

Replace a slice ?
s = "○◍○○" s[2..2] = '●' "○●○○"
s = "○◍○○" s[2..2] = "●" "○●○○"
s = "○◍○○" s[2..2] = "●●●●●" "○●●●●●○○"
s = "○◍◍○" s[2..3] = "●●" "○●●○"
s = "○◍◍◍" s[2..$] = "●●●●●" "○●●●●●"
new topic     » goto parent     » topic index » view message » categorize

3. Re: [Phix Tutorial] Basic Sequence Actions

_tom, can you explain the usage of a "gap?"

I've seen gaps like [2..1], but can there be other gaps, like [4..2]? And what does that do?

new topic     » goto parent     » topic index » view message » categorize

4. Re: [Phix Tutorial] Basic Sequence Actions

euphoric said...

_tom, can you explain the usage of a "gap?"

I've seen gaps like [2..1], but can there be other gaps, like [4..2]? And what does that do?

Gap | Item | Slice ( Caret | Cursor )

In a classic text based word processor a cursor "selects by highlighting a block on top of a character." In graphical word processors a caret "selects by a vertical line marker just before a character."

sequence s = "1234"  -- a string with four digit characters
"1❷34"  -- cursor  "on 2"
"1|234"   -- caret "before 2"

In a sequence|string we can use these ideas. In "1❷34" we say s[2] is the item 2 . In "1❷34" we say s[2..2] is the slice {2} . In "1|234" we say s[2..1] is the gap {} .

We can identify all gaps in a sequence as "|●|●|●|●|" ; there is a gap at the head and tail of a sequence; there is a gap between any two items. All gaps are an empty sequence {} or "" .

There is no item [0] or item [5] in the four item sequence "●●●●" , but there are lots of gaps.

                     Gaps in a Sequence or String ?                   
s = "|◍○○○" s = s[1..0] ""
s = "○|◍○○" s = s[2..1] ""
s = "○○|◍○" s = s[3..2] ""
s = "○○○|◍" s = s[4..3] ""
s = "○○○○|" s = s[5..4] ""

Each gap is an empty sequence.

To assign an atom to an empty gap is futile-- nothing happens.

An assignment will replace an empty gap with a new slice--the sequence grows by the length of the slice.

Reminder: This is a Phix tutorial. In oE you can not assign to a gap because the length of {} is zero and the length of anything interesting is >0 . In oE assignments must have equal lengths.

_tom

new topic     » goto parent     » topic index » view message » categorize

5. Re: [Phix Tutorial] Basic Sequence Actions

euphoric said...

_tom, can you explain the usage of a "gap?"

I've seen gaps like [2..1], but can there be other gaps, like [4..2]? And what does that do?

[4..3] is a "gap"

[4..2] is meaningless in oE|Phix

That suggests an "empty reverse slice" which is pushing syntax too far.

_tom

new topic     » goto parent     » topic index » view message » categorize

6. Re: [Phix Tutorial] Basic Sequence Actions

_tom said...
euphoric said...

_tom, can you explain the usage of a "gap?"

I've seen gaps like [2..1], but can there be other gaps, like [4..2]? And what does that do?

We can identify all gaps in a sequence as "|●|●|●|●|" ; there is a gap at the head and tail of a sequence; there is a gap between any two items. All gaps are an empty sequence {} or "" .

I think this concept really calls for a new representation. While the a[b-1..b] format works, I think it would be better to have a gap symbol (maybe).

All the gaps of a[1,2,3,4] could be:

a[%1]

a[%2]

a[%3]

a[%4]

a[%5]

I'm not saying the '%' is good for this, and I'm not saying it isn't. I just wanted something easy on the fingers for gaps. grin

_tom said...

To assign an atom to an empty gap is futile-- nothing happens.

Why? That seems arbitrary.

new topic     » goto parent     » topic index » view message » categorize

7. Re: [Phix Tutorial] Basic Sequence Actions

euphoric said...
_tom said...

To assign an atom to an empty gap is futile-- nothing happens.

Why? That seems arbitrary.

It is (at least) logically consistent:

string s = "abcde" 
s[2..4] = '3' -- s is "a333e" (3 elements replaced) 
s[2..3] = '2' -- s is "a223e" (2 elements replaced) 
s[2..2] = '1' -- s is "a123e" (1 element replaced) 
s[2..1] = '0' -- s is "a123e" (0 elements replaced) 
new topic     » goto parent     » topic index » view message » categorize

8. Re: [Phix Tutorial] Basic Sequence Actions

petelomax said...

It is (at least) logically consistent:

string s = "abcde" 
s[2..4] = '3' -- s is "a333e" (3 elements replaced) 
s[2..3] = '2' -- s is "a223e" (2 elements replaced) 
s[2..2] = '1' -- s is "a123e" (1 element replaced) 
s[2..1] = '0' -- s is "a123e" (0 elements replaced) 

Ah, OK.

What does s[2..4] = "hello" do?

I'll guess that s = { 'a', "hello", 'e' } now.

"0 elements replaced" seems to be a non sequitur.

You can't "replace 0 elements." IOW, if the element doesn't exist, it can't be replaced.

Any "replacement" should always replace at least one element. A replacement means something is taken away and something takes its place. Otherwise, it's an insertion. And gap "replacements" aren't replacements... they are insertions. You're "inserting into the gap."

I wonder if the gap would be useful to OE.

Anyway, I'm not working for change; just wanting to make sure I understand and everything works optimally. grin

new topic     » goto parent     » topic index » view message » categorize

9. Re: [Phix Tutorial] Basic Sequence Actions

euphoric said...

What does s[2..4] = "hello" do?

I'll guess that s = { 'a', "hello", 'e' } now.

No, you get "ahelloe" (on phix only), but you can (if you want) get that result from s[2..4] = {"hello"}

euphoric said...

You can't "replace 0 elements."

That's like saying you can't add 0 to a number. Obviously you can, it just does nothing.

new topic     » goto parent     » topic index » view message » categorize

10. Re: [Phix Tutorial] Basic Sequence Actions

Designing a language is about finding patterns. If you follow patterns (whatever they are) the language ends up being "simple." Lisp is perfect example of this.

Another strategy is to invent "clever syntax" as you go along. You can look at most popular conventional languages to see how this works.

Euphoria stated that "only positive integers may be used for index values." Since Euphoria 1.0 fits on a floppy disk this was a pragmatic choice.

Phix lets you use negative integers for indexing. Then Phix follows its own patterns.

Assigning a value to an empty sequence {} is a discard action.

{} = 'y'  -- works to discard a value
"" = 'y' -- FAILS to discard a value

Comment: 
If {} is equivalent to "" for "empty sequence"  
we must observe that some patterns may fail. 

Assigning an atom to a gap

                     Atom to Gap                  ?                   
s = "◍○○○" s[1..0] = '●' "◍○○○"
s = "○◍○○" s[2..1] = '●' "○◍○○"
s = "○○◍○" s[3..2] = '●' "○○◍○"
s = "○○○◍" s[4..3] = '●' "○○○◍"
s = "○○○○" s[5..4] = '●' "○○○○"

Phix allows negative integers for indexing.

                     Sequence to Gap              ?                   
s = "◍○○○" s[1..0] = "●" "●◍○○○"
s = "○◍○○" s[2..1] = "●" "○●◍○○"
s = "○○◍○" s[3..2] = "●" "○○●◍○"
s = "○○○◍" s[4..3] = "●" "○○○●◍"
s = "○○○○" s[5..4] = "●" "○○○○●"
s = "◍○○○" s[-4..-5] = "●" "●◍○○○"
s = "○◍○○" s[-3..-4] = "●" "○●◍○○"
s = "○○◍○" s[-2..-3] = "●" "○○●◍○"
s = "○○○◍" s[-1..-2] = "●" "○○○●◍"
s = "○○○○" s[0..-1] = "●" "○○○○●"

As billiard balls

                     Atom to slice                ?                   
s = "○○○○○" s[2..4] = '●' "○●●●○"
s = "○○○○○" s[2..3] = '●' "○●●○○"
s = "○○○○○" s[2..2] = '●' "○●○○○"
s = "○○○○○" s[2..1] = '●' "○○○○○"

An atom fills a slice on assignment. Once an atom is assigned to a "gap" it is extinguished.

So, Phix follows patterns. Patterns make writing code easier.

_tom

new topic     » goto parent     » topic index » view message » categorize

11. Re: [Phix Tutorial] Basic Sequence Actions

The idea of "gap" only came to me a few days ago. But it seems to help visualize how sequence works.

_tom

new topic     » goto parent     » topic index » view message » categorize

12. Re: [Phix Tutorial] Basic Sequence Actions

_tom said...

The idea of "gap" only came to me a few days ago. But it seems to help visualize how sequence works.

_tom

When using the elastic frame/billiard ball imagery as a beginner to visualise how sequences look and how they work, i distinguished between the structure of a sequence and its syntax, because sometimes the two don't correspond.

For example, i saw something like this somewhere in the docs:

    sequence s = { object1, 
               object2, 
               object3, 
               $ 
             } 

... and i kept wondering how to visualise the $ as part of the structure of the sequence. In the end i realised i couldn't! I had to visualise the structure of the sequence as ||object1|object2|object3||, and its syntactical representation as written above (where $ just meant the index of the last element). From then on I was free to imagine elements of sequences barging their way between other elements (eg as insertions or splices), or at the head of the line (eg prepend) or at the end of the line (eg append), etc. I didn't imagine any gaps between elements of sequences, or between the elastic frame and the first or last element; I'm still getting used to thinking in terms of gaps in the structure of sequences - it isn't intuitive for me yet, but who knows...

The syntax s[ n...n-1 ] to produce an empty sequence, is another example of sequence syntax not necessarily corresponding to sequence structure.

_Arch

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu