Post Reply 
Simple challenge in RPL, Extract the odd elements from a list
08-16-2017, 06:27 PM
Post: #5
RE: Simple challenge in RPL, Extract the odd elements from a list
(08-16-2017 01:41 PM)Werner Wrote:  
Code:
\<<
  1
  \<< NSUB 2 MOD NOT DROPN \>>
  DOSUBS
\>>

Werner's is probably the most flexible standard RPL approach you will find. It doesn't care if the total number of elements is even or odd, it leaves just the odd ones in the result.

(08-16-2017 01:50 PM)pier4r Wrote:  I though DavidM made a command to specify multiple position to pick elements from a list (like { 3 5 7 9 11 13 } { 2 4 } LGET -> { 5 9 } . Note that in this case LGET would be short of a sort of generalized SUB ) but it is not the case, so at most one can use LHDTL to go through the list.

If I was to use the ListExt library AND I knew that number of objects in the list was even, I would use this to extract the odd ones:
Code:
2 LDST HEAD
Changing HEAD to TAIL would result in the even-numbered elements. Or for a more "general purpose" approach, you could get every "nth" element (provided the list size is an even multiple of n) by placing n on the stack and executing the following:
Code:
LDST LRLL HEAD
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Simple challenge in RPL - compsystems - 08-16-2017, 12:54 PM
RE: Simple challenge in RPL, Extract the odd elements from a list - DavidM - 08-16-2017 06:27 PM



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