Post Reply 
Adding an entry to a vector or list
09-24-2023, 01:05 PM (This post was last modified: 09-24-2023 01:21 PM by krischik.)
Post: #1
Adding an entry to a vector or list
Yes, there is an archived thread on the subject but that thread only describes literal valued of known length.

My problem is lightly different: I have a vector V of unknown length (including 0) and want to append value E.

I could use a list as well for my purpose but can't get that to work as well. Start would be the the empty vector or list which I could not get to work either. I always hat to add a dummy element and delete that dummy element it later.

Is there a way to make this work without any hacks like dummy elements and one line/row matrix? Which is what I currently do. But I would like for something more elegant.

What I tried and didn't work:

Code:

EXPORT …
BEGIN
  LOCAL Retval := {};

  FOR …
    LOCAL Z0 := …

    APPEND (Retval, Z0); 
  END;

  RETURN Retval;
END;

Code:

EXPORT …
BEGIN
  LOCAL Retval := [];

  FOR …
    LOCAL Z0 := …

    APPEND (Retval, Z0); 
  END;

  RETURN Retval;
END;

This works but is not very elegant:

Code:

EXPORT …
BEGIN
  LOCAL Retval := [[0]];

  FOR …
    LOCAL Z0 := …

    ADDCOL (Retval, [Z0], rowDim (Retval) + 1); 
  END;

  DELCOL (Retval, 1);
  RETURN Retval[1];
END;

https://uiq3.sf.net/rpn-45/
Developer RPN-45, FX-602P Simulator, FX-603P Simulator
HP16C, HP35s, HP-Prime, FX-602P, FX-603P, PC-1403



Visit this user's website Find all posts by this user
Quote this message in a reply
09-24-2023, 01:21 PM (This post was last modified: 09-24-2023 01:22 PM by Didier Lachieze.)
Post: #2
RE: Adding an entry to a vector or list
For a list you can do list(0):=E and it will add the value of E as the last element of the list, even if the list is empty.

In your example it would be Retval(0):=Z0;
Find all posts by this user
Quote this message in a reply
09-24-2023, 01:36 PM
Post: #3
RE: Adding an entry to a vector or list
(09-24-2023 01:21 PM)Didier Lachieze Wrote:  For a list you can do list(0):=E and it will add the value of E as the last element of the list, even if the list is empty.

Wow. That actually works. Thank you very much.

https://uiq3.sf.net/rpn-45/
Developer RPN-45, FX-602P Simulator, FX-603P Simulator
HP16C, HP35s, HP-Prime, FX-602P, FX-603P, PC-1403



Visit this user's website Find all posts by this user
Quote this message in a reply
09-24-2023, 04:13 PM
Post: #4
RE: Adding an entry to a vector or list
Bonjour

Vous pouvez aussi utiliser list(-1):=expr, dans ce cas expr sera ajouté en tête de liste.
Ce qui pour une liste vide revient au même Smile


Hello

You can also use list(-1):=expr, in which case expr will be added to the top of the list.
Which for an empty list is the same Smile

Sorry for my english
Find all posts by this user
Quote this message in a reply
09-25-2023, 05:35 AM
Post: #5
RE: Adding an entry to a vector or list
(09-24-2023 04:13 PM)Tyann Wrote:  You can also use list(-1):=expr, in which case expr will be added to the top of the list.
Which for an empty list is the same Smile

Good to know. So much missing or difficult to find in the manual.

https://uiq3.sf.net/rpn-45/
Developer RPN-45, FX-602P Simulator, FX-603P Simulator
HP16C, HP35s, HP-Prime, FX-602P, FX-603P, PC-1403



Visit this user's website Find all posts by this user
Quote this message in a reply
09-25-2023, 11:22 PM
Post: #6
RE: Adding an entry to a vector or list
(09-24-2023 04:13 PM)Tyann Wrote:  You can also use list(-1):=expr, in which case expr will be added to the top of the list.

Is negative index HOME table insertion documented anywhere?

This is very confusing, positive index for table assignment, non-positive index for table insertion.
Table lost its nice boundary checks, with possibly nasty bug, especially if index were computed.

Also, if index 0 is for end-of-list, -1 should really be one before, not head-of-list. It made no sense.

Anyway, this is Lua simulation of how HOME table behave. (based from trial and error)
Note that "=" is in quote, it is not assignment, but insertion! How confusing!

I think table insertion should really be a separate function, like Lua's table.insert

Code:
home_meta = {
    __index = function(a,i)                  -- t[non-existent i]
        if i==0 then return rawget(t,#t) end -- t[0] == t[#t]
        return "Error: " .. (i<0 and "Bad argument type" or "Invalid Input")
    end,
    __newindex = function(t,i,v)             -- t[non-existent i] "=" v
        i = i==0 and (#t+1) or abs(i)        -- t[0] == t[#t+1]
        if i <= #t then return table.insert(t,i,v) end        
        for j = #t+1, i-1 do rawset(t,j,0) end; rawset(t,i,v)        
    end,   
}


lua> home_table = function(t) return setmetatable(t, home_meta) end

lua> t = home_table {1,2,3}
lua> t[1], t[2], t[3], t[0] -- home table lookup behavior, t[0] ≡ t[#t]
1      2      3      3
lua> t[-1], t[4]               -- home table error msg for bad indexes
Error: Bad argument type      Error: Invalid Input

lua> ;require'pprint'        -- home table insertion / extension behavior
lua> pprint(t)
{ 1, 2, 3 }
lua> t[0]=5; pprint(t)
{ 1, 2, 3, 5 }
lua> t[-4]=4; pprint(t)
{ 1, 2, 3, 4, 5 }
lua> t[-10]=10; pprint(t)
{ 1, 2, 3, 4, 5, 0, 0, 0, 0, 10 }

FYI, last operation crashed HP Prime emulator.
Find all posts by this user
Quote this message in a reply
09-26-2023, 05:26 AM
Post: #7
RE: Adding an entry to a vector or list
Bonjour Albert.

PPL dispose d'une fonction INSERT qui fonctionne sur les listes et sur les chaînes.
Mais sur les chaînes seulement pour un seul caractère (je ne comprends pas pourquoi une telle restriction).
Enfin sur ma G2 physique version 2.1.14730, L1(-10):=1 par exemple provoque un redémmarrage.


Hi Albert.

PPL has an INSERT function that works on lists and strings.
But on strings only for a single character (I don't understand why such a restriction).
Finally, on my physical G2 version 2.1.14730, L1(-10):=1 for example causes a restart.

Sorry for my english
Find all posts by this user
Quote this message in a reply
09-26-2023, 07:15 AM
Post: #8
RE: Adding an entry to a vector or list
(09-25-2023 11:22 PM)Albert Chan Wrote:  
(09-24-2023 04:13 PM)Tyann Wrote:  You can also use list(-1):=expr, in which case expr will be added to the top of the list.

Is negative index HOME table insertion documented anywhere?

This is very confusing, positive index for table assignment, non-positive index for table insertion.

Anyway, this is Lua simulation of how HOME table behave. (based from trial and error)

lua> pprint(t)
{ 1, 2, 3 }
lua> t[0]=5; pprint(t)
{ 1, 2, 3, 5 }
lua> t[-4]=4; pprint(t)
{ 1, 2, 3, 4, 5 }
lua> t[-10]=10; pprint(t)
{ 1, 2, 3, 4, 5, 0, 0, 0, 0, 10 }

FYI, last operation crashed HP Prime emulator.

(09-26-2023 05:26 AM)Tyann Wrote:  
Finally, on my physical G2 version 2.1.14730, L1(-10):=1 for example causes a restart.

Thanks for the posts. Lost of good points made. I've filed a bug report for list insertion (via ":=" with a negative index) well past the current end of a list... and... the bug got fixed! Big Grin

Also: the list assignment syntax allows for a sequence of indices (for nested assignment / insertion).
Find all posts by this user
Quote this message in a reply
09-26-2023, 11:19 AM
Post: #9
RE: Adding an entry to a vector or list
Bonjour

Je ne savais pas que l'indice négatif permet d'insérer une valeur.
Je ne connaissais que -1 en tête de liste, mais effectivement
si L1:={1,2,3,4,5} --> L1(-2):=-1 renvoie {1,-1,2,3,4,5}
Bon à savoir.


Hello

I didn't know that the negative subscript could be used to insert a value.
I only knew about -1 at the top of the list, but in fact
if L1:={1,2,3,4,5} --> L1(-2):=-1 returns {1,-1,2,3,4,5}
Good to know.

Sorry for my english
Find all posts by this user
Quote this message in a reply
09-27-2023, 12:53 PM
Post: #10
RE: Adding an entry to a vector or list
(09-25-2023 11:22 PM)Albert Chan Wrote:  
(09-24-2023 04:13 PM)Tyann Wrote:  You can also use list(-1):=expr, in which case expr will be added to the top of the list.

Is negative index HOME table insertion documented anywhere?

This is very confusing, positive index for table assignment, non-positive index for table insertion.
Table lost its nice boundary checks, with possibly nasty bug, especially if index were computed.

Also, if index 0 is for end-of-list, -1 should really be one before, not head-of-list. It made no sense.

I do not like this either. Using a negative number to indicate insertion rather than replacement is confusing and non-standard. In many languages, particularly Python (another Prime language option), a negative index indicates offset from the end of the list rather than the beginning. I can't see a reason for this odd notation as opposed to the more standard insert.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 6 Guest(s)