1. Append

Need help understanding append

	object pc = {{},{}} 
	pc[1] = "1234"  
	pc[2] = "2345" 
-- prints 2 
	puts(1,"len pc = " & to_string(length(pc)) & "\n") 
	for x = 1 to length(pc) do  
-- prints 1234, then 2345 
			puts(1, to_string(pc[x]) & "\n") 
	end for 
	pc = append(pc,pc) 
-- print 3; i thought it would be 2 
	puts(1,"len pc after append = " & to_string(length(pc)) & "\n") 
	pc[2][1] = "3456"  
	pc[2][2] = "4567" 
	for x = 1 to length(pc) do 
		for y = 1 to 2 do 
-- prints 1,2 then 3456,4567, then 1234, 2345 
-- I had planned on it printing 1234, 2345 then 3456, 4567 
			puts(1,pc[x][y] & "\n") 
		end for 
	end for 
 
new topic     » topic index » view message » categorize

2. Re: Append

buzzo said...

Need help understanding append

	object pc = {{},{}} 
	pc[1] = "1234"  
	pc[2] = "2345" 
-- prints 2 
	puts(1,"len pc = " & to_string(length(pc)) & "\n") 
	for x = 1 to length(pc) do  
-- prints 1234, then 2345 
			puts(1, to_string(pc[x]) & "\n") 
	end for 
	pc = append(pc,pc) 
-- print 3; i thought it would be 2 
	puts(1,"len pc after append = " & to_string(length(pc)) & "\n") 
	pc[2][1] = "3456"  
	pc[2][2] = "4567" 
	for x = 1 to length(pc) do 
		for y = 1 to 2 do 
-- prints 1,2 then 3456,4567, then 1234, 2345 
-- I had planned on it printing 1234, 2345 then 3456, 4567 
			puts(1,pc[x][y] & "\n") 
		end for 
	end for 
 

Historically the append(seq, obj) function has added one object to the tail end of the sequence. Thus the append function always adds one and only one to the length of the sequence.

So in your code the following line produces the sequence shown below it (Note that the entire original pc sequence is appended to the end.):

	pc = append(pc,pc) 
{ {1,2,3,4}, {2,3,4,5}, {{1,2,3,4}, {2,3,4,5}} } 
 

Going forward I believe it was decided to modify append to append(obj, obj) where if the first object is an atom/integer the function will turn that into a sequence which contains the single value and then it will append the second object to that sequence.

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

3. Re: Append

try

pc = append({pc},pc) 

Check out example 2 for Append in the OpenEuphoria manual

Maybe an example showing this could be added to the manual?

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

4. Re: Append

Solved with this code .. print output is as expected.

         
object pc = {{},{}} 
	object pc2 = {} 
 
	pc2 = append(pc2,pc) 
	pc2[1][1] = "2345" 
	pc2[1][2] = "3456" 
	 
	pc2 = append(pc2,pc) 
	pc2[2][1] = "3456"  
	pc2[2][2] = "4567"	 
 
	pc2 = append(pc2,pc) 
	pc2[3][1] = "5678"  
	pc2[3][2] = "6789"		 
 
	for x = 1 to length(pc2) do 
		for y = 1 to 2 do 
			puts(1,pc2[x][y] & "\n") 
		end for 
	end for 
new topic     » goto parent     » topic index » view message » categorize

5. <eucode>Re: Append

	object pc = { {{},{}} }    -- pc is a sequence of sequences which contains two sequences 
	pc[1][1] = "1234"   
	pc[1][2] = "2345"  
-- prints 2  
	puts(1,"len pc = " & to_string(length(pc)) & "\n")  
	for x = 1 to length(pc) do   
		for y = 1 to 2 do  
-- prints 1234, then 2345  
			puts(1, to_string(pc[x][y]) & "\n")  
		end for  
	end for  
	pc = append(pc,pc[1])  
-- print 3; i thought it would be 2  
	puts(1,"len pc after append = " & to_string(length(pc)) & "\n")  
	pc[2][1] = "3456"   
	pc[2][2] = "4567"  
	for x = 1 to length(pc) do  
		for y = 1 to 2 do  
-- prints 1,2 then 3456,4567, then 1234, 2345  
-- I had planned on it printing 1234, 2345 then 3456, 4567  
			puts(1,pc[x][y] & "\n")  
		end for  
	end for  
 
new topic     » goto parent     » topic index » view message » categorize

6. Re: Append

buzzo said...

Need help understanding append

I have to suggest adding

with trace 
trace(1)  

at the top of your program, or replace all those prints with ?pc like so:

object pc = {{},{}}  
?pc -- {{},{}} 
        pc[1] = "1234"   
?pc -- {"1234",{}} 
        pc[2] = "2345"  
?pc -- {"1234","2345"} 
        pc = append(pc,pc)  
?pc -- {"1234","2345",{"1234","2345"}} 
        pc[2][1] = "3456"   
?pc -- {"1234",{"3456",51,52,53},{"1234","2345"}} 
    -- aka {"1234",{"3456",'3','4','5'},{"1234","2345"}} 
        pc[2][2] = "4567"  
?pc -- {"1234",{"3456","4567",52,53},{"1234","2345"}} 
    -- aka {"1234",{"3456","4567",'4','5'},{"1234","2345"}} 
buzzo said...
	pc = append(pc,pc) 

I suspect the statement you really wanted there was

        pc = {pc,pc} 

but I cannot be entirely certain from the limited information given blink

HTH,
Pete

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

7. Re: Append

buzzo said...

Solved with this code .. print output is as expected.

         
object pc = {{},{}} 
	object pc2 = {} 
 
	pc2 = append(pc2,pc) 
	pc2[1][1] = "2345" 
	pc2[1][2] = "3456" 
	 
	pc2 = append(pc2,pc) 
	pc2[2][1] = "3456"  
	pc2[2][2] = "4567"	 
 
	pc2 = append(pc2,pc) 
	pc2[3][1] = "5678"  
	pc2[3][2] = "6789"		 
 
	for x = 1 to length(pc2) do 
		for y = 1 to 2 do 
			puts(1,pc2[x][y] & "\n") 
		end for 
	end for 

... and I would code that as follows

        object pc2 = {}  
  
        pc2 = append(pc2,{"2345","3456"})  
        pc2 = append(pc2,{"3456","4567"}) 
        pc2 = append(pc2,{"5678","6789"}) 
  
        for x = 1 to length(pc2) do  
                for y = 1 to 2 do  
                        puts(1,pc2[x][y] & "\n")  
                end for  
        end for  

Same results

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

8. Re: Append

I have been thinking of adding a syntax extension that would work not only for builtins but also user created routines.

In EUPHORIA, barring psuedo-memory routines like you find in std/map.e, when something is modified in a routine, said object needs to be returned in this routine to the caller or such changes will not be in the result.

Consider the idea we allow syntax like:

s append= a 

and let it be equivialent to

s = append(s, a) 

This is alright for append, remove_item, remove, and prepend, but for store, and apply and others which require other arguments this syntax wouldn't do.

Suppose we make expressions like:

   spanish_dictionary store=({2}, "poner") 

and let it be equivalent to:

  spanish_dictionary  = store(spanish_dictionary, {2}, "poner") 

.

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

9. Re: Append

SDPringle said...

I have been thinking of adding a syntax extension that would work not only for builtins but also user created routines.

My first reaction was great! SDPringle can invent, and implement, new ideas.

Delayed reaction is: my head might explode. Euphoria does not take kindly to shortcuts and new syntax.

To show that you are working with a subroutine you need an argument list, possibly empty, shown by ( ) parenthesis.

s = append(s, pair ) 
s append= (pair) 
 
s = store(s, key, data ) 
s store=(key,data) 

Allowing () to "vanish" is a nice shortcut that adds another rule, an exception, that must be learned. Extra congnitive effort means not simple. The result is that

--either 
s append= pair     -- breaks a pattern, so it is a problem 
 
-- or 
s store=(key,data) -- adds a pattern, so it is a problem 

Will I get confused between a binary operator and a two argument function?

-- two argument function 
 
x = append(x,3) 
x append=(3) 
 
 
-- binary operator ... two arguments 
 
x = x + 2 
x += 2 
 
-- another kind of two argument function 
 
function add( atom a, atom b ) 
	return a+b 
	end function 
	 
x = add(2,3) 
x add=(2,3) 
	-- the previous pattern suggests that somehow "add" must be a three argument function 
	-- with the first argument being "x" 
	-- but, that is not what I have in add(2,3) 

My point is that I, thats one viewpoint, will make various typing errors in working with this syntax.

Then again, if you had dot notation, then the transition to your shortcut notation might be easy to visualize.

s = append(s, pair ) 
s = s.append( pair ) 
s append=(pair) 
 
s = store(s, key, data ) 
s = s.store( key, data ) 
s store=(key,data) 

In dot notation the "self" argument moves to a new location but does not vanish. In the shortcut notation the "self" argument vanishes. If something vanishes, then it is not Euphoric syntax.

_tom


Forked into: Dot notation

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

10. Re: Append

buzzo said...

Need help understanding append

Here is some content that I may add to the documentation. If you had seen this before you started writing your code would it have helped?

_tom


Matrix

A matrix is "used in mathematics where items, numbers or other mathematical objects, are arranged in a rectangular pattern. Operations like addition and multiplication are defined for a matrix."

An array is "a computer data-type that provides a rectangular arrangement of items, numbers or expressions, which are all the same data-type."

In Euphoria you can use a sequence to represent a matrix or an array. Euphoria is especially flexible since an item may be any Euphoria object.

A matrix written in mathematical notation looks like:

-- mathematical matrix A 
    ┌            ┐ 
A = │  4      5  │ 
    │  0.1  333  │ 
    └            ┘ 

The same matrix written as a Euphoria sequence:

-- Euphoria sequence A  
sequence A = {  
               {   4, 5  }, 
               { 0.1, 333} 
             } 

Subscript notation is "used to identify a particular item in a matrix or sequence."

Mathematical subscript notation looks like:

-- subscript notation 
    ┌                ┐ 
A = │  a        a    │ 
    │   1,1      1,2 │ 
    │                │           
    │  a        a    │ 
    │   2,1      2,2 │ 
    └                ┘ 

Euphoria subscript notation looks like:

-- subscript notation 
sequence A = {  
               { A[1,1], A[1,2] }, 
               { A[2,1], A[2,2] } 
             } 

A scalar operation "like addition or multiplication, applies to every item in a matrix or sequence." A matrix operation "like matrix multiplication or transposition, requires a specific algorithm for each case." Use the keyword "matrix" to locate include files for matrix operations from The Archive.

-- examples scalar operators with sequences 
-- example algorithm for a matrix operation 

Building a Matrix

A matrix is described by its dimensions which is the number of m rows followed by the number of n columns. Dimensions are written using mXn notation to describe an m-by-n matrix.

Use the { , , , } sequence formation operator to build any sequence explicitly.

A flat sequence is equivalent to a 1Xn matrix which could also be call one row.

-- a 1X2 matrix 
-- also called a "row" 
 
object a,b  -- items in the matrix 
           
           
A = {a,b} 

After defining a row you can use it to build a sequence with more dimensions. The repeat function lets build a matrix with many rows:

object a=1,b=2 
sequence row = { a, b } 
 
sequence A = repeat( row, 2 )  -- a 2X2 matrix 
 
? A 
--{ 
--   {1,2}, 
--   {1,2} 
-- } 
 
sequence B = repeat(row, 4 )  -- a 2X4 matrix 
 
? B  
 
-- { 
--   {1,2}, 
--   {1,2}, 
--   {1,2}, 
--   {1,2} 
-- } 
 

To add one row at a time use the append function. The "trick" is that you must put the first row inside a sequence

sequence row = { 0, 0 } 
sequence matrix = {} 
 
-- add one row to the matrix 
matrix = append(matrix, row) 
	--> matrix is { {0,0} } 
	 
-- add another row to the matrix 
matrix = append(matrix, row ) 
	--> matrix is { {0,0}, {0,0} } 

A "trick shortcut" idiomatic to Euphoria is to write:

sequence row = {0,0} 
sequence matrix = append( {row}, row ) 
 
--> matrix is { row, row } 
--> { {0,0}, {0,0} } 

You must remember that the append function adds just one item to the list of items in a sequence.

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

11. Re: Append

_tom said...
buzzo said...

Need help understanding append

Here is some content that I may add to the documentation. If you had seen this before you started writing your code would it have helped?

_tom

I appreciate your efforts to improve the documentation, it can be a thankless task. If you do it poorly everybody gripes and if you do it well almost nobody notices.

That said, I think you are over thinking this a bit. Not all would be programmers have had matrix math. Some are still in grade school.

I think everyone can understand the idea of a list, I was sent home each week with a spelling word list starting in the first grade.

A sequence in euphoria is simply a list of objects. An object in euphoria is either an individual number (atom, integer) or a sequence (see the topic of data types).

So append just adds one more object to the end of a sequence (list of objects).

The second parameter supplied to the append function is the object to be added.

When the first parameter supplied to the append function is a sequence then the second parameter is added to the end of that sequence.

When the first parameter is an atom, the append function first creates a sequence with this atom as the sole object in it. Then the object passed as the second parameter is added to this created sequence.

   a = append(1,2)          -- a would contain {1,2}       The first parameter is an atom, so is the second parameter 
   b = append(1,{2})        -- b would contain {1,{2}}     The first parameter is an atom, the second is a sequence 
   c = append({},{1,2})     -- c would contain {1,2}       The first parameter was an empty sequence (zero objects) and the object {1,2} is added. 
   d = append({1,2},{3,4})  -- d would contain {1,2,{3,4}} The first parameter is a sequence, the object {3,4} is added to that sequence 
 
new topic     » goto parent     » topic index » view message » categorize

12. Re: Append

buzzo said...

Solved with this code .. print output is as expected.

Hi buzzo. I'm not sure if you are still concerned with how append() works or not. Sure, your solution works but its not really the best way to do what it looks like you are trying to achieve.

Here is a more straight forward approach...

         
object pc = {} 
 
pc = append(pc, {"2345", "3456"}) 
pc = append(pc, {"3456", "4567"}) 
pc = append(pc, {"5678", "6789"}) 
 
for x = 1 to length(pc) do 
	for y = 1 to 2 do 
		puts(1,pc[x][y] & "\n") 
	end for 
end for 
new topic     » goto parent     » topic index » view message » categorize

13. Re: Append

SDPringle said...

I have been thinking of adding a syntax extension that would work not only for builtins but also user created routines.

  • Why would this shortcut (the concept rather than the syntax) be a good idea?
  • How will it help the coder of the source code?
  • How will it help the reader of the source code?
  • How will it help the parser produce better IL code?
new topic     » goto parent     » topic index » view message » categorize

14. Re: Append

mindwalker said...

I appreciate your efforts to improve the documentation, it can be a thankless task. If you do it poorly everybody gripes and if you do it well almost nobody notices.

Thankyou.

_tom


I like the the idea of a sequence as a "list."

The promotion of an atom to a sequence when used with prepend/append is not working in 4.1AL . This just illustrates the problems in creating documentation.

The documentation will need a response to the fact that Euphoria does not have an array data-type. The matrix description is one possible start on this topic.

Here are some ideas to describe "append" and related topics:


Sequence Anatomy

This is how we describe a sequence:

 

                      first       last           │  list items 
                        ↓           ↓            │ 
                                             ┐   ┘ 
                        1   2   3   4        │  
                                    $        │      subscript INDEX values 
                                    length(s)
         ┌                                   ┘          
sequence │    s = {     a,  b,  c,  d           }       

--                                           ┐  
--                   0  1   2   3   4  5     │      function ARGUMENT values 
--                                           ┘  ┐ 
--                   ↑                 ↑        │  
--                 head               tail      │   to inset or connect new items 
--                                              ┘         

An index is "an integer ranging from one to the length of the sequence; Euphoria uses one-based indexing." The first item is "indexed as s[1]." The last item is "indexed as s[$] which is the same as s[length(s)] ."

The head of a sequence is "the empty space before the first item s[1] ."

The tail of a sequence is "the empty space after the last item s[$] ."

The length "for a sequence is the count of the top-level items in a sequence; an empty sequence has 0 zero length." Use the length function to get the length of any data-object. The length of an atom is 1 one.

Insetting: prepend, insert, append

The insert function has three arguments: who, what, where.

  • Who: the sequence; the target of the renovation.
  • What: item; data-object to be inset.
  • Where: index; future location for the new item.

The insert function insets a new item just before the where argument of the target sequence.

If it helps, you can pretend that the head and tail are "invisible" items in a sequence list. That explains why you can use function argument values, like 0 zero, that are outside the values allowed for item indexing.

--             "head"                       "tail" 
--               ↓                            ↓            
sequence s = {         item1, item2, item3         } 

The functions prepend (inset before first item) and append (inset after last item) are special cases of the the insert function. All insetting functions increase length by one.

  • prepend insets an item at the head
  • insert insets anywhere in a sequence
  • append insets an item at the tail
INSET Head Anywhere Tail
prepend prepend(s,x)
insert ◄ ▼ ► insert(s,x, 1) insert(s,x,i) insert(s,x, length(s)+1)
append append(s,x)

The append function is probably the most commonly used function. Each time you use append you add one item to the end of your sequence list.

Example 1:
-- the possible insert locations 
 
sequence s = {1,2,3} 
for i=0 to length(s)+1 do 
    ? insert(si, 99, i ) 
end for 
 
-- the output is 
 
-- {99,1,2,3} 
-- {99,1,2,3}      -- same as prepend(s,99) 
-- {1,99,2,3} 
-- {1,2,99,3} 
-- {1,2,3,99}      -- same as append(s,99) 
Example 2:
-- prepend 
-- inset at "head" before first item 
 
s = { 'a', 'b', 'c', 'd' } 
s = prepend(s, 100} 
--> s is { 100, 'a', 'b', 'c', 'd' } 
 
-- same as prepend 
S = { 'a', 'b', 'c', 'd' } 
S = insert(s, {"cat","dog"}, 1) 
--> S is { {"cat","dog"}, 'a', 'b', 'c', 'd' } 
Example 3:
-- append 
-- inset at "tail" after last item 
 
s = { 'a', 'b', 'c', 'd' } 
s = append(s, 100} 
--> s is { 'a', 'b', 'c', 'd', 100 } 
 
-- same as append 
S = { 'a', 'b', 'c', 'd' } 
S = insert(s, {"cat","dog"}, length(S)+1 ) 
--> S is { 'a', 'b', 'c', 'd', {"cat","dog"} } 

A common use for append is to make a list of lists. It helps if you start with an empty list:

Example 4:
sequence mylists = {} 
sequence list1 = { "12", "23" } 
sequence list2 = { "34", "45" } 
 
mylists = append(mylists, list1 ) 
	--> mylists is  { { "12", "23" } } 
	 
mylists = append(mylists, list2 ) 
	--> mylists is {  
	--               { "12", "23" }, 
	--               { "34", "45" }  
	--              } 
Example 5:

A commonly used Euphoria idiom for making a list of lists is to write:

sequence mylists  
sequence list1 = { "12", "23" } 
sequence list2 = { "34", "45" } 
 
mylists = append( { list1 }, list2 ) 

The result is the same as Example 4.

Possible Future enhancement
The prepend and append functions are especially flexible when used with atoms. If the first argument is an atom it will be promoted to a one item sequence; then the second argument is inset as one more item.

Example 6:
/* 

? prepend(3,12)       -- atom 3 is not promoted to sequence {3} 
--> error 
-- first argument of prepend must be a sequence  
*/ 
 
? prepend( {3}, {12} ) 
--> {{12},3} 
 
 
? append( {}, {'a','b'} )  -- object inset into the tail of empty sequence 
--> { {'a','b'} } 
 
? append( {'a','b'}, {} )  -- empty sequence inset into the tail of a sequence 
--> {'a','b',{}} 
 

Connect: &, splice, &

Connecting "adds two objects together to make a longer object." The final length is the sum of the two original lengths.

CONNECT Head Anywhere Tail
& x & s
splice ◄ ▼ ► splice(s,x, 1) splice(s,x,i) splice(s,x, length(s)+1)
& s & x

Connecting with &

Concatenation is "a binary operation using the & concatenation operator that joins two objects together seamlessly." When two atoms are concatenated they make a two item sequence. When an atom is concatenated to a sequence the atom is added as one item. Sequences are connected tail to head.

You can join a object x to the head of object s by writing x & s, or you can join object x to the tail of object s by writing s & x.

While we do not consider a number to have length it is convenient to think of an atom as having a length of one. This is because one atom concatenated to a sequence increases length by one; two atoms concatenated together make a sequence of length two.

Any data-object is just one item in a sequence; an atom, sequence, string, or nested sequence are all one item; each has a length of one.

Example 1:
sequence s = 2 & 8 
--> s is { 2, 8 }  -- length 1 + 1 is two 
 
sequence S = { 'a', 'b', 'c', 'd' } 
S = S & 100 
--> S is { 'a', 'b', 'c', 'd', 100 }  -- length 4+1 is five 
--> S is "abcdd"                        -- 'd' is ASCII 100 
 
sequence g = "Hello" 
g = g & " Euphoria" 
--> g is "Hello Euphoria"   -- length 5+9 is fourteen 
 
sequence x = { "hello", 10, -2 } 
x = { "open", "Euphoria } & x 
--> x is { "open", "Euphoria, "hello", 10, -2 }  -- length 2+3 is five 

Connecting with splice

The splice function will "connect an object, seamlessly, anywhere in a sequence list; the new object appears before the 'index' value used as an argument."

The & concatenation operator and splice function both increase the length of a sequence by the length of object being connected.

Concatenating x & s is the same as splice(s,x,1) where the new object is connected before the first item.

Concatenating s & x is the same as splice(s,x,length(s)+1) where the new object is connected after the last item.

Example 2
-- the possible splice locations 
 
sequence si = { 1, 2, 3 } 
 
for i=0 to length(si)+1 do 
	? splice(si, {99,99,99}, i ) 
end for 
 
-- the output is 
 
-- {99,99,99,1,2,3} 
-- {99,99,99,1,2,3}    -- same as {99,99,99} & {1,2,3} 
-- {1,99,99,99,2,3} 
-- {1,2,99,99,99,3} 
-- {1,2,3,99,99,99}    -- same as {1,2,3} & {99,99,99} 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu