Post Reply 
Programming puzzles: processing lists!
01-27-2019, 05:51 PM
Post: #248
RE: Programming puzzles: processing lists!
(01-27-2019 11:49 AM)pier4r Wrote:  Stumbled on an expected behavior
...

This topic comes up from time to time, and is the source of confusion for many people. I suppose we'll never know why the original RPL designers decided to make "+" for lists a concatenation command instead of following the model already used by -, *, and /. Since ΣLIST uses repeated applications of "+", it shares the same fate.

Your proposed alternative is exactly what I would do in that situation.

(01-27-2019 11:49 AM)pier4r Wrote:  Another point is that one has a list of sublists of the same dimension and one would like to know the average value of a particular position across the sublists. Say
{ { 1 2 3 } { 2 3 4 } { 3 4 5} } what is the average value in the 3rd position of the sublists?

Just using the built-in commands, you could do something like the following:
Code:
\<<
   1 \<< 3 GET \>> DOSUBS
   DUP \GSLIST SWAP SIZE /
\>>

That is reasonably compact, and I believe fairly straightforward in terms of readability.

When I read the phrase "a list of sublists of the same dimension" followed by the idea of performing numerical processing on the same data, I also start to think that it may be better to use an array instead of a list. It depends on the processing you need to perform, of course, but some of the array commands may be handy here.

Let's first convert the list to an array with AXL:
Code:
{ { 1 2 3 } { 2 3 4 } { 3 4 5} } AXL
...which yields:
[[ 1 2 3 ]
[ 2 3 4 ]
[ 3 4 5 ]]

Now we can use COL- to extract the third column. Since we aren't concerned with the rest of the array, that can be discarded with NIP:
Code:
3. COL- NIP
...which yields:
[ 3 4 5 ]

We can then determine the average (or some other result) as needed. In this particular case, the average takes a couple more steps than with a list, but it's not too bad. Other types of numeric processing may actually be simpler with an array than if the data is in list form:
Code:
\<<
   OBJ\-> EVAL
   DUP \-> count
   \<<
      2 SWAP START + NEXT
      count /
   \>>
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - DavidM - 01-27-2019 05:51 PM



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