INSERT doesn't insert - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: INSERT doesn't insert (/thread-13681.html) |
INSERT doesn't insert - Cristóbal De Jesús - 09-18-2019 02:13 PM Code: EXPORT STRINGIDS() I tried the INSERT command with lists, in the Home screen, and it doesn't seem to insert items to lists. Most likely, I am missing something here... RE: INSERT doesn't insert - toml_12953 - 09-18-2019 04:14 PM (09-18-2019 02:13 PM)Cristóbal De Jesús Wrote: The INSERT statement doesn't change the list. It only returns the value. You have to set the list equal to the value yourself. If you're familiar with function vocabulary, the Prime only passes parameters by value, not by reference. Personally, if I could only pass one way, I'd pass by reference. Ideally, the interpreter would give you the option of passing variable either way like Microsoft BASIC does with the BYVAL keyword. Passing by value makes a copy of the parameter to use in the function. You can't change the value of the parameter because you're only working with a copy. Passing by reference passes the address of the variable to the function so you're working directly with the variable. This means if you change the variable in the function, the value will be changed in the main program. This uses less memory since you don't have to make copies of variable (especially arrays!) Only a pointer to the variable is passed. It is usually faster to pass by reference as well since you don't have the overhead of copying the value of the variable to a new location. Since Prime passes by value, when you use INSERT(STRGS,1,STRG), you add STRG to a copy of STRGS, not the original STRGS. That's why you have to do the STRGS:= because you have to save the return value of the function. If Prime passed by reference, it would work the way you expected and the way I'd like it to and the := wouldn't be necessary. There is a danger to passing by reference, however. You can change your original variables which makes recursion difficult and you might change variables inadvertently. Code: EXPORT STRINGIDS() RE: INSERT doesn't insert - cyrille de brébisson - 09-19-2019 05:10 AM Hello, INSERT works like this on purpose... first, it allows insert to work on a list which is the result of an expression, without having to first store it somewhere... like: insert(L1*2,4,4); or something like this... second, implementing a sto in an expression is actually hard to implement in prime for a number of reasons... Cyrille RE: INSERT doesn't insert - Cristóbal De Jesús - 09-19-2019 10:26 PM I'm getting the concept of passing parameters by value or by reference, thanks for the thoughtful reply. And thanks for explaining the design reasoning as well. RE: INSERT doesn't insert - Carlos295pz - 09-20-2019 01:09 PM In lists it is possible to insert using a negative index: Code: INSERT(STRGS,1,STRG) → STRGS(-1):=STRG Positive indexes to modify or create Index 0 to insert at the end or call the last item List as a parameter to extract part of the list: L1 ({2,5}) |