Re: Using insert()
- Posted by _tom (admin) Jan 16, 2019
- 1412 views
Append | Concatenate
An append "introduces one new item to the end of a sequence list." An append increases the length of a sequence by one.
s = { â– , â– , â– , â– } -- introducing action s = append(s, â–¡ } ? s --> { â– , â– , â– , â– , â–¡ }
sequence s s = {1,2,3,4} s = append(s, 99 ) ? s --> {1,2,3,4, 99 } -- one item introduced s = {1,2,3,4} s = append(s, {99} ) ? s --> {1,2,3,4, {99} } -- one item introduced
A prepend "introduces one new item to the start of a sequence list." A prepend works at the start of the sequence list (it is like an append).
A concatenation "joins two objects together." After joining, the final length is equal to the sum of the two original lengths.
s1 = { â– , â– , â– , â– } s2 = { â–¡ , â–¡ } -- joining action ? s1 & s2 --> { â– , â– , â– , â– , â–¡ , â–¡ }
sequence s1, s2 s1 = {1,2,3,4} s2 = {50,60} ? s1 & s2 --> {1,2,3,4,50,60}
Insert | Splice
An insert "introduces one item into a sequence." You can introduce an item at the head of a sequence (use insert or prepend), within a sequence (insert), or at the tail of a sequence (insert or append). The prepend|append actions are very common so they get their own functions. The length of a sequence after an insert increases by one.
s = { â– , â– , â– , â– } s = insert(s, â–¡ , 2 ) ? s --> { â– , â–¡ , â– , â– , â– } s = { â– , â– , â– , â– } s = insert(s, 5 ) ? s --> { â– , â– , â– , â– , â–¡ } -- an append is the same as an insert to length+1 s = { â– , â– , â– , â– } s = append(s, â–¡ ) ? s --> { â– , â– , â– , â– , â–¡ }
sequence s s = {1,2,3,4} s = insert(s, 999, 2 ) ? s --> {1, 999, 2,3,4} s = {1,2,3,4} s = insert(s, 999, 5 ) ? s --> {1,2,3,4, 999 } s = {1,2,3,4} s = insert(s, 999, 1 ) ? s --> { 999, 1,2,3,4}
A splice "joins the ends of objects together." Sum the lengths of the spliced objects to get the length of the new sequence.
s = { â– , â– , â– , â– } s = splice(s, { â–¡ , â–§ }, 2 ) ? s --> { â– , â–¡ , â–§ , â– , â– , â– } s = { â– , â– , â– , â– } s = splice(s, { â–¡ , â–§ }, 4 ) ? s --> { â– , â– , â– , â– , â–¡ , â–§ } -- a splice at the tail is the same as concatenation & s = { â– , â– , â– , â– } s = { â– , â– , â– , â– } & { â–¡ , â–§ } ? s --> { â– , â– , â– , â– , â–¡ , â–§ }
sequence s s = {1,2,3,4} s = splice(s, {66,99}, 2 ) ? s --> {1, 66,99, 2,3,4 } s = {1,2,3,4} s = splice(s, {66,99}, 5 ) ? s --> {1,2,3,4, 66,99 } s = {1,2,3,4} s = s & {66,99} ? s --> {1,2,3,4, 66,99 }
string | sequence
A string is "a flat sequence of atoms." Each atom being an integer code for plain text or UTF encoding.
If you insert something other than an atom (as a valid code) into a string-- you no longer have a valid string.
str = "â—â—â—â—â—â—â—â—" -- a string str = insert(str, {"bad"}, 3 ) ? str --> { â— , â— , {"bad"}, â— , â— , â— , â— , â— , â— } -- no longer a string