Post Reply 
Programming puzzles: processing lists!
06-12-2017, 02:21 PM (This post was last modified: 06-12-2017 02:21 PM by DavidM.)
Post: #138
RE: Programming puzzles: processing lists!
(06-12-2017 08:37 AM)pier4r Wrote:  So aside creating my own debug friendly dosubs/dolist (I am not so motivated) what could be workarounds since I need to iterate over a list?

DOSUBS and DOLIST aren't "debuggable" in the usual manner. The only way I know of to debug this sort of thing is to simply write a test scenario where the stack is pre-loaded with sample contents and then you would put your << ... >> executeable on the stack for evaluation. Tedious, but you can at least step through things to see what happens with one iteration. You could always create a "feeder" app to call that one with several sample inputs.

(06-12-2017 08:37 AM)pier4r Wrote:  Iterating with a FOR index L1 index GET NEXT is super slow for the quick test I made. (maybe in other 50g languages is faster) ... Another alternative still is to explode the list before the iteration, and then working on it with stack operations. But that is very messy in stack terms. One has to debug carefully.

I usually try to avoid GET and PUT operations in any kind of looped operation for that very reason. "Explode-iterate-implode" requires care, but the payoff in performance is good. Most commands in the library I'm working on make use of either that model or a loop of "biting off" one element at a time.

(06-12-2017 08:37 AM)pier4r Wrote:  - L1 listHead: returns on stack 2 the list, without the head, on stack 1 the head of the list
{ 1 2 3} -> { 2 3 } 1

I could put something like this in the library, but it's still fairly easy (and efficient) in UserRPL as well:

DUP TAIL SWAP HEAD

(06-12-2017 08:37 AM)pier4r Wrote:  - L1 listTail: returns on stack 2 the list, without the tail, on stack 1 the tail of the list
{1 2 3 } -> {1 2} 3

Similar to the above, but requires a couple of extra REVLISTs. Fortunately, that command is fast (even for large lists):

REVLIST DUP TAIL REVLIST SWAP HEAD

(06-12-2017 08:37 AM)pier4r Wrote:  - L1 <num> listHeadIterate: returns the first <num> elements from the head of the list (in case it repeats making circle of the list), plus the list rotated (so the num+1 element now is in the head).
{ 1 2 3 4 5 } 2 -> { 3 4 5 1 2 } 1 2
- L1 <num> listTailIterate: returns the first <num> elements from the tail of the list, and then the list rotated
{ 1 2 3 4 5 } 2 -> { 4 5 1 2 3 } 5 4
- those above could be extended on multiple lists. To partially substitute DOLIST .

This is similar to a combination of (NEG) LROT with GoferList's Take, though the latter command returns its results in a list. I'm not sure what this would look like with multiple lists; could you provide an example?
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - DavidM - 06-12-2017 02:21 PM



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