Add a value at the start of a list.
|
01-10-2024, 10:57 PM
(This post was last modified: 01-10-2024 11:41 PM by matalog.)
Post: #1
|
|||
|
|||
Add a value at the start of a list.
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/~par...#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]. |
|||
01-11-2024, 12:04 AM
(This post was last modified: 01-11-2024 12:15 AM by StephenG1CMZ.)
Post: #2
|
|||
|
|||
RE: Add a value at the start of a list.
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-94...t=list+api (If you need to use [], there may be be a way, or you can convert to a list and then back again). Stephen Lewkowicz (G1CMZ) https://my.numworks.com/python/steveg1cmz |
|||
01-11-2024, 12:07 AM
(This post was last modified: 01-11-2024 12:28 AM by komame.)
Post: #3
|
|||
|
|||
RE: Add a value at the start of a list.
(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 |
|||
01-11-2024, 12:50 AM
Post: #4
|
|||
|
|||
RE: Add a value at the start of a list.
Thanks for the explanation.
|
|||
01-12-2024, 11:13 PM
Post: #5
|
|||
|
|||
RE: Add a value at the start of a list. | |||
01-13-2024, 01:15 AM
Post: #6
|
|||
|
|||
RE: Add a value at the start of a list.
(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} <0|ΙΈ|0> -Joe- |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)