Post Reply 
list functions sugestion
09-20-2015, 07:55 PM (This post was last modified: 09-20-2015 08:40 PM by hpfx.)
Post: #1
list functions sugestion
Hello,
I've functions suggestion, it's related to list in programs.

GET(L1,n)
get n-th item of the list
ex :GET({1, 2, 3},2) -> 2

FOREACH L1 AS var;
END;

to iterate lists items.
ex: FOREACH {1,2} AS it; PRINT(it);END;
note : or with alternative syntax (less block style) : FOREACH(L1,var); END;


Arr2List() and List2Arr()
convert array (vectors, matrix) to list and reverse..
ex:Arr2List([1, 2]) -> {1, 2}
Arr2List([[1, 2][3,4]]) -> {{1, 2}{3,4}}


Let me know your feeling about such suggestion, I do really like to have a foreach function like in many modern language, it makes life simplier to iterate into lists.

And please, add a Lists menu in Cmds (program editing), to show existing list command like SIZE, SUB, POS ...
Otherwise it's not obvious o guess such functions does exists...
Find all posts by this user
Quote this message in a reply
09-20-2015, 08:41 PM (This post was last modified: 09-20-2015 08:44 PM by komame.)
Post: #2
RE: list functions sugestion
Hi,

To get n-th item of the list you can use index:
local l; //declare variable for the list
l:={1, 2, 3}
l(2) -> 2
you can also store values in the n-th item of list that way:
l(2):=4
PRINT(l) => {1, 4, 3}

FOREACH? => there is the ITERATE function, but not for all purposes.

Arr2List and List2Arr? => Check list2mat and mat2list.

BR
Find all posts by this user
Quote this message in a reply
09-20-2015, 08:49 PM (This post was last modified: 09-20-2015 08:56 PM by hpfx.)
Post: #3
RE: list functions sugestion
thank you konamz,
In the mean time, I got found for SIZE (I edited my post)
but you helped me for other.
Indeed, most did already exist, that's nice!

(09-20-2015 08:41 PM)komame Wrote:  FOREACH? => there is the ITERATE function, but not for all purposes.
ITERATE is related to an expression.
I would like to suggest a FOREACH function, not to extend existing ITERATE.
in case you don't know foreach (like in php for example), it's nothing related to hp prime interate.
First, it's a block function (like a while), it goes throu each item on that way :
Code:
FOREACH {1, 2, 3 } AS var;
// do something with var 
END;
we can also imagine FOREACH would access by reference to the item, that mean, modifying var would change the list itself !
example
Code:
L1:={4, 6, 9, 3};
FOREACH L1 AS it;
if(it MOD 2 == 0) it=it-1;
END;
// now L1 is { 3, 5, 9, 3 }
Find all posts by this user
Quote this message in a reply
09-20-2015, 08:53 PM (This post was last modified: 09-20-2015 08:54 PM by komame.)
Post: #4
RE: list functions sugestion
Yes, FOREACH is very modern function and there is no equivalent in HP PRIME, but you can do workaround using FOR:

Code:
local list = {1, 2, 3 };
FOR x FROM 1 TO SIZE(list) DO
PRINT(list(x));
END;
Find all posts by this user
Quote this message in a reply
09-20-2015, 08:59 PM
Post: #5
RE: list functions sugestion
thank you for your answers,
(09-20-2015 08:53 PM)komame Wrote:  Yes, FOREACH is very modern function and there is no equivalent in HP PRIME
That's why I suggested it Wink
hp prime is modern too Wink
Find all posts by this user
Quote this message in a reply
09-21-2015, 07:15 AM
Post: #6
RE: list functions sugestion
Hello,

It should be possible to write a User Function for the FOREACH list function using a few lines of code. Could some kind person write such a function and place it in the library here for the benefit of all.

Thank You
Colm
Find all posts by this user
Quote this message in a reply
09-21-2015, 09:15 AM
Post: #7
RE: list functions sugestion
Inside CAS, you can do e.g.
l:=[1,2,4];
for j in l do print(j); end;
Find all posts by this user
Quote this message in a reply
09-22-2015, 05:46 AM
Post: #8
RE: list functions sugestion
Hello,

It depends on what you are trying to achieve, but on Prime, you can do:
function(list) and function will apply to each element of the list.
This acts like a FOREACH.
But it is limited to math functions...

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
09-22-2015, 08:04 AM (This post was last modified: 09-22-2015 08:05 AM by hpfx.)
Post: #9
RE: list functions sugestion
Hello,
(09-22-2015 05:46 AM)cyrille de brébisson Wrote:  Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
You probably work on hp prime calculators at hp (let's hope), wowww...

(09-22-2015 05:46 AM)cyrille de brébisson Wrote:  Hello,

It depends on what you are trying to achieve, but on Prime, you can do:
function(list) and function will apply to each element of the list.
This acts like a FOREACH.
But it is limited to math functions...

Cyrille
I didn't know that.
That's already interresting, but it was more to cover programming stuff.
Here is a (stupid) example:
Code:
L1:={4, 6, 9, 3};
FOREACH L1 AS itm;
   if(itm MOD 2 == 0) itm=itm-1;
END;
 // now L1 is { 3, 5, 9, 3 }

Of course, I know it can be achieved by
Code:

// These 2 lines replace suggested "FOREACH L1 AS itm"
FOR n FROM 1 TO SIZE(L1) DO
itm:=L1(n);

   // here goes block content

// these 2 lines replace suggested "END;"
L1(n):=itm;
END;

As well as all loops (FOR...) can be replaced by a simple WHILE cmd.
But we all find it conenient to use proper syntax for each loops.

If you at hp, you think it could be convenient to add this loop type, it would be nice, and I would like it. And it would make the hp-basic more modern, btw.

Of course, it's just an idea that can be extented to matrix, strings...

Thank you for your attention.
Find all posts by this user
Quote this message in a reply
09-22-2015, 11:06 AM (This post was last modified: 09-22-2015 11:07 AM by Tyann.)
Post: #10
RE: list functions sugestion
Bonjour
Ce qui serait bien aussi :
Insert(Liste,position,liste ou valeur)
et
Delete(liste,position,[nombre d'éléments])
S'il vous plait

Hello
What would be great also:
Insert ( list , position ,value or list )
and
Delete (list, position, [ number of elements ] )
please

Sorry for my english
Find all posts by this user
Quote this message in a reply
09-22-2015, 01:25 PM (This post was last modified: 09-23-2015 04:54 AM by komame.)
Post: #11
RE: list functions sugestion
If you want insert a value for a vector you can use the ADDCOL:
ADDCOL([1,2,3,4],9,3) => [1,2,9,3,4]
But there is no equivalent command for lists.

I agree that INSERT and DELETE commands should be added for lists and vectors, and also strings.

BR,
Piotr
Find all posts by this user
Quote this message in a reply
09-23-2015, 05:48 AM
Post: #12
RE: list functions sugestion
hello

>L1:={4, 6, 9, 3};
>FOREACH L1 AS it if(it MOD 2 == 0) it=it-1; END;
>// now L1 is { 3, 5, 9, 3 }

L1-((L1 MOD 2)==0) does that

L1 MOD 2 does a modulo 2 of each element of L1
the ==0 will return a list of 0 and 1, with 1 for every element of L1 that is 0
and the final - removes that 1 in cases of equality mod 0 from L1

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
Find all posts by this user
Quote this message in a reply
09-23-2015, 10:52 PM
Post: #13
RE: list functions sugestion
(09-22-2015 11:06 AM)Tyann Wrote:  Hello
What would be great also:
Insert ( list , position ,value or list )
and
Delete (list, position, [ number of elements ] )
please

Here are two functions doing that:

Code:
// Insert 'value' at 'position' in 'list'. Value can be a list or a single value.
// If position is <=1, value is inserted at the beginning of the list
// If position is after the last element of the list, value is inserted at the end of the list

EXPORT InsList(list,position,value)
BEGIN
  CASE
    IF position<=1 THEN CONCAT(value,list);END;
    IF position>SIZE(list) THEN CONCAT(list,value);END;
    DEFAULT CONCAT(CONCAT(list({1,position-1}),value),list({position,SIZE(list)}));
  END;
END;

Code:
// Delete a 'qty' of elements from 'list' starting at 'position'.
// The elements to be deleted are at the intersection of [1,size(list)] and [position, position+qty-1] 
//    for ex. DelList({1,2,3,4},0,3) deletes the first and second elements of the list and returns {3,4}

EXPORT DelList(list,position,qty)
BEGIN
  LOCAL lb,le;
  IFTE(position<=1,lb:={}, lb:=list({1,position-1}));
  IFTE(position+qty>SIZE(list), le:={}, le:=list({position+qty,SIZE(list)}));
  CONCAT(lb,le);
END;
Find all posts by this user
Quote this message in a reply
09-23-2015, 11:14 PM
Post: #14
RE: list functions sugestion
I would like to see a LOOKUP function
So given a list like
((Fred, England),(Armstrong,USA))
I can do
entry := lookup(1,"Fred")
or
...lookup(2,"USA").
maybe returning position rather the list entry.

It would be useful for both lists and spreadsheets.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
09-23-2015, 11:54 PM (This post was last modified: 09-23-2015 11:56 PM by Didier Lachieze.)
Post: #15
RE: list functions sugestion
With L1:={{"Fred", "England"},{"Armstrong","USA"}}
you can do
entry := POS(L1(1),"Fred")
or
...POS(L1(2),"USA")
Find all posts by this user
Quote this message in a reply
09-24-2015, 02:53 PM (This post was last modified: 09-24-2015 02:53 PM by hpfx.)
Post: #16
RE: list functions sugestion
(09-23-2015 05:48 AM)cyrille de brébisson Wrote:  L1-((L1 MOD 2)==0) does that

Impressive ! really...

But it was just a dummy example,
of course the idea is to have several commands inside FOREACH bloc.
Otherwise it can be tricky to find such expression to do the job each time.
Find all posts by this user
Quote this message in a reply
Post Reply 




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