Post Reply 
Programming puzzles: processing lists!
06-12-2017, 08:37 AM (This post was last modified: 06-12-2017 10:05 AM by pier4r.)
Post: #137
RE: Programming puzzles: processing lists!
So while I'm attacking the #33 I see a similar problem to the #20 (that it is still not solved in RPL, by me at least).

With the #20 I am stuck on debug problems in case of nested iterations on a list, like:

Code:

- given L1
for i in L1; do
  ...code...
  for j in L1; do
    ...code...
  done
  ...code...
done

If I try to do this with nested DOSUBS/DOLIST either I am super careful, or debugging it is very taxing.
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?

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)

The library of David gave me an idea (maybe an idea also for him?). Another way to iterate over a list is to rotate the list and get the head. Or consume the list (from the head/tail).
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.

Therefore I try to ask some more functions for the David Library, of course it is a wish no pressure whatsoever.

Having list operations (that I partially coded in userRPL) to iterate over a list in several ways:

- 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
- 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
- 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 .
- other possible functions to iterate over/consume a list (a collection of lists)? (will be interesting to compare those with FOR + get , of course dolist/dosubs will be always faster)

I believe if I go through processing challenges using David's library, I'll find a couple more requests for sure. Again, I don't want to make pressure, those are just wishes. (I also believe that through -reasonable- wishes a library can grow very useful)

Main source repository changed the url: https://app.assembla.com/spaces/various-...Operations


Also #22
Code:

c22vaV1
    @Given two list of positive integers. 
    @ Add the elements (with equal position in the lists ) 
    @if those are different, don't add if those are equal.
    
    @it seems to work.
    \<<
      
      \->
      @input
      inputL1
      inputL2
      
      @local var
      \<<
        inputL1
        inputL2
        2 @pick one element from 2 lists
        \<<
          IF
            DUP2 ==
          THEN
            DROP @drop one
          ELSE
            +
          END
        \>>
        DOLIST
      \>>
    \>>

also #33
Code:

c33vaV2
    @ See V1 for metadata and notes
    @to avoid DOSUBS/DOLIST when operations are complicated
    @ it seems to work
    @ 11:52 12.06.2017 I can get even faster noticing that the
    @ element in position I gets children in position 2I and 2I+1
    @ timing is needed, after general timing of
    @ FOR + GET, dosubs, dolist, start+headlist
    \<<
      0 "l1Size" DROP
      0 "noMoreLeavesBool" DROP
      1 "counter1" DROP
      2 "counter2" DROP
      1 "counter3" DROP
      0 "source" DROP
      0 "dest" DROP
      { } "inputL1loop1" DROP
      { } "inputL1loop2" DROP
      0 "inputL1loop2size" DROP
      { } "resList" DROP
    
      \->
      @input
      inputL1
      
      @local var
      l1Size
      noMoreLeavesBool
      counter1
      counter2
      counter3
      source
      dest
      inputL1loop1
      inputL1loop2
      inputL1loop2size
      resList
      \<<
        inputL1 SIZE 'l1Size' STO
      
        @using a FOR/WHILE instead of dolist is way more flexible.
        @dosusbs shows the number of elements processed as well, but
        @well, not as flexible as a For, and not as debug friendly.
        @still the most common operations with FOR are slow.
        
        inputL1 'inputL1loop1' STO
        inputL1 headList DROP
        'inputL1loop2' STO
        l1Size 1 - 'inputL1loop2size' STO
        
        WHILE
          inputL1loop2size 0 >
        REPEAT
          inputL1loop1 headList
          'source' STO
          'inputL1loop1' STO
          
          1 'counter3' STO
          
          IF
            counter1 1 ==
          THEN
            @if it is the first element
            'resList' 0 source 2 \->LIST 1 \->LIST
            STO+
              @ we need double list to keep sublists with the "+" operation
              @TIL, I cannot just build a list within RPL, like { { 0 source } }, 
              @ I need to build it
              @programmatically. 11:42 12.06.2017
          END    
            
          
          @iterate on the rest of the elements
          WHILE
            counter3 3 <
            inputL1loop2size 0 >
            AND
          REPEAT
            inputL1loop2 headList
            'dest' STO
            'inputL1loop2' STO
            
            'resList' source dest 2 \->LIST 1 \->LIST
            STO+
            1 'counter3' STO+
            'inputL1loop2size' 1 STO-
          END
          
          'counter1' 1 STO+
        END
        
        resList
      \>>
    \>>

Wikis are great, Contribute :)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Programming puzzles: processing lists! - pier4r - 06-12-2017 08:37 AM



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