Do a procedure on a subset of a list
The DOSUBS command EVALuates a specified program (or a variable which contains one) to groups of elements in a list.
The first iteration uses elements 1 through In from the list; the second iteration uses elements 2 through In + 1; and so on. In general, the m-th iteration uses the elements from the list corresponding to positions m through m + In – 1.
During an iteration, the position of the first element used in that iteration is available to the user using the local variable NSUB, and the number of groups of elements is available using the local variable ENDSUB.
DOSUBS is nominally designed for Pexec (or Nexec) requiring In arguments and returning one result.
DOSUBS is not fully equivalent to its userRPL counterpart.
In particular:
NSUB and ENDSUBS are commands; in newRPL they are local variables;DOSUBS operates in a protected stack environment: no elements present on the stack before DOSUBS is launched can be reached or altered.Input
{ 1 2 3 4 5 }
2
« → a b
« CASE
'NSUB==1'
THEN
a
END
'NSUB==ENDSUB'
THEN
b
END
'a+b' EVAL
END
»
»
DOSUBS
Output
4:
3:
2:
1: { 1 5 7 5 }
…………………………………………………………………………………………………………
| Command | Short Description | |
|---|---|---|
| →LIST | Assemble a list from its elements | |
| LIST→ | Split a list into its elements | |
| DOLIST | Do a procedure with elements of lists | CHANGED |
| DOSUBS | Do a procedure on a subset of a list | CHANGED |
| MAP | Do a procedure on each element of a list, recursively | |
| MAPLIST→ | Do a procedure on each element recursively, return individual elements | NEW |
| STREAM | Do a procedure on consecutive elements of a list | |
| ΔLIST | First differences on the elements of a list | |
| ΣLIST | Sum of all elements in a list | CHANGED |
| ΠLIST | Product of all elements in a list | CHANGED |
| ADD | Concatenate lists and/or elements | CHANGED |
| SORT | Sort elements in a list | |
| REVLIST | Reverse the order of elements in a list | |
| ADDROT | Add elements to a list, keep only the last N elements | NEW |
| SEQ | Assemble a list from results of sequential procedure |