48G: A little list-processing help?



#12

On level 2 I have a list, say {2 3 5 7 11 13 17}.

On level 1 I have a list of index numbers, {1 3 5 6}.

As output I'd like a list of the first, third, fifth and sixth elements in the level-2 list, namely {2 5 11 13}.

How do I do that, in general?

Thanks for a lesson.


#13

Hi.

I cannot figure a solution for now, but I'd suggest reading about DOLIST and DOSUBS.

Sorry not helping some more.

Cheers.

Luiz (Brazil)

#14

I never needed the list processing functions,

so in plain UserRPL I'd do it like this:

«  -> a b
« { } 1 b SIZE
FOR i
a b i GET GET +
NEXT
»
»

Not very elegant, but straightforward;-)


#15

As written in my earlier post, the obvious loop version doesn't look
as elegant as the DOSUBS/DOLIST versions.
However, internally those are loops, too.

Slightly OT, but just for the fun, here's a much faster SysRPL version.

::  CK2
DUP1LAMBIND
LENCOMP
#1+_ONE_DO (DO)
DUP ( *L2 L2* )
1GETLAM ( *L2 L2 L1* )
INDEX@ NTHCOMPDROP ( *L2 L2 i* )
%ABSCOERCE
NTHCOMPDROP ( *L2 i'* )
SWAP ( *i' L2* )
LOOP

DROP
1GETABND
LENCOMP
{}N
;

This could be made even faster using meta objects, but the necessary offset calculations would also make the code somewhat more complicated, at least for this example;-)


#16

For those still interested, I made some timing measurements.

Timings are as follows:

UserRPL Loop: 161ms

DOLIST version: 159.28ms

DOSUBS1: 173.693ms

DOSUBS2: 139.635ms

SysRPL Loop: 22.2ms

DOSUBS1 is the slowest version, which also leaves the input list in level 2,
and thus is different to all other solutions.

DOSUBS2 is faster than the other UserRPL solutions since it only uses one implicit LAM binding.

The DOLIST version and the DOSUBS2 version are essentially the same,

except that the DOLIST inner workings are slower than those of DOSUBS in this case.

The SysRPL version is much faster because much of the UserRPL overhead is not there.


#17

Thank's for the benchmark !

I think that with a more complex example, the difference between "FOR" and "DOSUBS" or "DOLIST" will be greater.

In general I noticed that the use of {} followed by numerous + is very slow. Best way is to use the stack and a ->LIST command at the end to collect all the datas.

In my opinion, the 'ideal' implementation would be simply :

{2 3 5 7 11 13 17}
{1 3 5 6}
GET

-> { 2 5 11 13 }

But GET don't work that way, on the contrary of other commands. (probably becaus GET already accept a {} structure for the POS argument in some situations)

But in general, it works, and without DOSUBS or DOLIST... For exemple, try :

{ 0 10  20 30 40 } SIN
(0,0) {(1,1) (3,2) (5,5)} LINE
5 { 1 2 3 4 } /
{ 1 2 3 } { 4 5 6 } /
{ 0 1 1 0 1 } { 1 2 3 4 5 } IFT
123 { 'a' 'b' 'c' 'd' 'e' 'f'} STO
{ 1 2 3 } {'a' 'b' 'c'} STO
etc.

In some case, this is very powerfull

See AUR pages F-1

Edited: 27 June 2012, 1:38 p.m.

#18

You could probably speed up the SysRPL a bit more by replacing the SWAP and LOOP with a SWAPLOOP. If nothing else, it'll trim 2.5 bytes off the program size.

#19

Hi, there is a lot of way to do this. For example :

1 « NSUB 1 + PICK SWAP GET » DOSUBS

or


SWAP -> List
«
1 « List SWAP GET » DOSUBS
»

#20

Thanks to Luiz for encouragement, to Raymond for one lesson and to Gilles for two.

All three solutions work as claimed, and all three will teach me something. Maybe even a lot.

What a wonderful resource this forum is!

#21

Hi,

Here is another solution, using DOLIST. Just put your lists in stack levels 2 and 1, as described, and execute this program:

<< SWAP -> L
<< 1 << L SWAP GET >> DOLIST >>
>>

Best regards.


#22

Thank you, Pascal. Ma coupe déborde.

All the best,


Possibly Related Threads...
Thread Author Replies Views Last Post
  HP-Prime : Extracting elements from a list in RPN Miguel Toro 0 200 11-15-2013, 06:44 PM
Last Post: Miguel Toro
  [HP-Prime] Picking elements from a List in a program Jean-Michel 3 244 11-15-2013, 04:16 AM
Last Post: dg1969
  Hp PRIME - how to send a list to the connectivity Kit giancarlo 1 195 11-10-2013, 11:50 AM
Last Post: Tim Wessman
  How do I decompose a list with the HP Prime. Hal Bitton in Boise 4 249 11-08-2013, 02:07 PM
Last Post: Patrice
  HP-Prime/Xcas: vector/list indexing fhub 6 350 10-27-2013, 04:52 PM
Last Post: fhub
  Hp prime list function - indexing into a list Andy B (Australia) 3 231 10-07-2013, 01:41 AM
Last Post: cyrille de Brébisson
  [HP-Prime CAS] list[x,y,z], vector[x, y, z ] ... data type operation? CompSystems 1 176 08-22-2013, 03:30 PM
Last Post: Joe Horn
  [HP-prime] Parallel Processing, vectors, matrices as a only data type =( CompSystems 1 162 08-08-2013, 04:48 PM
Last Post: peacecalc
  HP 50G List of Library numbers already in use? Michael Lopez 2 191 08-04-2013, 10:10 PM
Last Post: Michael Lopez
  [HP -Prime CAS] List, Matrix, Vector as one Array? CompSystems 0 108 07-26-2013, 05:22 PM
Last Post: CompSystems

Forum Jump: