Re: [Phix Tutorial] Basic Sequence Actions
- Posted by _tom (admin) Jan 27, 2019
- 1442 views
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