Add a value at the start of a list. - 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: Add a value at the start of a list. (/thread-21148.html) |
Add a value at the start of a list. - matalog - 01-10-2024 10:57 PM Is there a way to add a value to the start of a list? Once I had asked if there was a way to delete a value at the start of a list, here on this forum, and was shown this page: https://www-fourier.ujf-grenoble.fr/~parisse/giac/doc/en/cascmd_en/#sec%3Aseq which has the command "insert()" which would be fine I think, but are those xcas commands usable on the HP Prime? I found CONCAT, but it doesn't seem to do what I need. If I had a list: L1:=CONVERT(123,"base",2); If I then CONCAT(L2,L1) I end up with L1={2,3,[1 1 1 1 0 1 1]}, but I actually want [2 3 1 1 1 1 0 1 1]. I'm pretty sure my terminology of some part of this is wrong here, I am getting mixed up between the [] type of lists and the {} type of lists. What is the difference? If I : CONCAT(MAKEMAT(0,3),[1 0 1]) I end up with {[0 0 0],[1 0 1]} instead of [0 0 0 1 0 1]. RE: Add a value at the start of a list. - StephenG1CMZ - 01-11-2024 12:04 AM CONCAT({1,2},{3,4}) -> {1,2,3,4} seems ok for lists, which in PPL have braces. The [] are matrices or vectors, though in cas I believe they can be mixed. Some XCAS commands are implemented, but not all. I have a collection of useful list functions here: https://www.hpmuseum.org/forum/thread-9411.html?highlight=list+api (If you need to use [], there may be be a way, or you can convert to a list and then back again). RE: Add a value at the start of a list. - komame - 01-11-2024 12:07 AM (01-10-2024 10:57 PM)matalog Wrote: Is there a way to add a value to the start of a list? The result of CONVERT is a vector, not a list. A vector is a part (row) of a matrix and can only take numerical values; it is organized differently in memory. Each record in a matrix must have the same number of values. On the other hand, a list is fully dynamic both in terms of the type of data it can store and can contain other lists, vectors, and text strings... To achieve the effect you're looking for, convert the CONVERT result into a list, and then CONCAT will work, as well as inserting values at the front using negative indices. Code: L1:=mat2list(CONVERT(123,"base",2)) and you will get in L1: {2 3 1 1 1 1 0 1 1} For single values, you can also use negative indices, for example: Code: L1:={2,3}; L1 => {5,2,3} (01-10-2024 10:57 PM)matalog Wrote: If I : CONCAT(MAKEMAT(0,3),[1 0 1]) I end up with {[0 0 0],[1 0 1]} instead of [0 0 0 1 0 1]. Here, you've created a vector, not a list. And after combining two vectors with CONCAT, you've obtained a two-element list that contains vectors in each of its elements. To create a list, you must use MAKELIST. For example: Code: CONCAT(MAKELIST(X, X, 1, 3), {1, 0, 1}) result => {1,2,3,1,0,1} Piotr RE: Add a value at the start of a list. - matalog - 01-11-2024 12:50 AM Thanks for the explanation. RE: Add a value at the start of a list. - BruceH - 01-12-2024 11:13 PM (01-10-2024 10:57 PM)matalog Wrote: Is there a way to add a value to the start of a list? Code: INSERT({1,2,3},1,5) -> {5,1,2,3} RE: Add a value at the start of a list. - Joe Horn - 01-13-2024 01:15 AM (01-12-2024 11:13 PM)BruceH Wrote:(01-10-2024 10:57 PM)matalog Wrote: Is there a way to add a value to the start of a list? Or, slightly simpler: Code: prepend({1,2,3},5) -> {5,1,2,3} |