Post Reply 
Programming puzzles: processing lists!
06-12-2017, 07:24 PM
Post: #140
RE: Programming puzzles: processing lists!
(06-12-2017 04:48 PM)pier4r Wrote:  I have the userRPL (that explodes the list, yours are very compressed in terms of code. Neat) of those commands, I wondered if making a sysRPL version would have been (way) faster.

Well in this case barely faster. The UserRPL commands aren't much different from the SysRPL ones in this particular case. But I have no problem adding this, so I went ahead and created a new command "LHDTL" -- here's the detailed description:

Code:
______________________________________________________________________________

LHDTL (List Head Tail)

Input
1: { list of 0 or more objects }

Output
2: { remaining elements in a list }
1: object (first element from original list)

Returns the first element from a list, leaving the remaining elements in place in a
list in stack level 2.  If the list is empty, returns an empty list and NOVAL.

Examples:
{ 1 2 3 4 5 } LHDTL => { 2 3 4 5 } 1
{ } LHDTL => { } NOVAL

I don't much like commands that leave varying levels of results, so I chose to return NOVAL for an empty list's first object instead of returning just an empty list. Hopefully that's an acceptable approach. Also, it isn't a stretch to see that "REVLIST LHDTL" will get the last element, and I figured that if you are processing a list from the end you'd probably want to keep it reversed anyway. So I didn't see as much value in adding a separate command to get the last element.

(06-12-2017 04:48 PM)pier4r Wrote:  Sure, it is all for iterations instead of using DOLIST
{ 1 2 3 4 5 } { 1 2 3 4 5 }
2 @num list
2 @pick elements from head and rotate lists
listHeadIterate

output
{3 4 5 1 2}
{3 4 5 1 2}
1
1
2
2

I've added this to my list of "future considerations". I need to think about this one some more.
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 07:24 PM



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