Path: republic.btigate.com!news-xfer.netaxs.com!news1.chicago.iagnet.net!iagnet.net!vixen.cso.uiuc.edu!newsrelay.iastate.edu!news.iastate.edu!vax1.miu.edu!JHMEYERS
From: jhmeyers@miu.edu (John H Meyers)
Newsgroups: comp.sys.hp48
Subject: DOLIST & DOSUBS for S/SX
Date: 21 Jan 1998 22:49:36 GMT
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Lines: 214
Message-ID: <6a5u20$gmc$1@news.iastate.edu>
Reply-To: jhmeyers@miu.edu
NNTP-Posting-Host: vax1.mum.edu
Xref: republic.btigate.com comp.sys.hp48:5915

S/SX owners:

  Have you ever found programs that were written for the G/GX
  using the handy DOLIST and DOSUBS functions, but you couldn't
  use them, because the S/SX does not have these functions?

  No problem! -- you can now run all those G/GX programs,
  with the DOLIST and DOSUBS programs provided below.

Note to G/GX owners:

  Sorry, this is not for you!  You can't even install these into your
  G/GX, so go and read your User's Guide (pages 17-4,5) and/or AUR,
  while we talk to the S/SX folks in private :)

---------------------------------------------------------------

DOLIST applies a program to all the elements of either one list or
a set of lists (each of which must have the same number of elements):

{ list N } ... { list 1 } N \<< program \>> DOLIST ==> { results }

DOLIST first takes the first element of each list (or the only list)
and puts it on the stack; then it executes the program you supplied,
which at your option may leave some results on the stack, or none.

Then DOLIST pushes the second element of each list onto the stack
(below any previous results), and executes the program again,
and so on until the last element of each list has been processed.

Each time your program executes, it can decide anew what to do
with the arguments (and previous results) which are on the stack;
you can, for example, choose to return some results some of the
time and not other times (perhaps to select some but not all
of the original list elements), or you can just do exactly
the same thing with every argument (or set of arguments).

After all list elements have been presented once each to your program,
DOLIST finally gathers up all the results you left on the stack into
one final list, with the exception that if you left no results at all
on the stack (as determined by whether or not the stack grew in length),
then *no* final list is returned (rather than an empty list).

DOLIST Examples:

{ 4 3 2 1 } 1 \<< SQ \>> DOLIST ==> { 16 9 4 1 }

{ 6 5 4 } { 3 2 1 } 2 \<< + \>> DOLIST ==> { 9 7 5 }

{ 6 5 4 } { 3 2 1 } 2 \<< ^ \>> DOLIST ==> { 216 25 4 }

0 { 1 2 3 4 5 } 1 \<< + \>> DOLIST ==> 15  [Sum of a List]

Select words in list-2 which are not in list-1:
\<< \-> e \<< 1 \<< e OVER POS { DROP } IFT \>> DOLIST \>> \>>

This program eliminates consecutive duplicate objects from any list:
\<< IF DUP SIZE THEN DUP 1 \<< DUP2 SAME { DROP } IFT \>> DOLIST
    SWAP DROP END \>>

This program (by Michael Hoppe) performs a "transpose" on a
"list of lists" representation of a matrix (symbolic or otherwise):
\<< LIST\-> DUP \-> n \<< \<< n \->LIST \>> DOLIST \>> \>>

Exercise: How do the above programs work?

---------------------------------------------------------------------

DOSUBS applies a program to groups of one or more elements taken from
just one input list:

  { list } N \<< program \>> DOSUBS ==> { results }

DOSUBS first puts the first N elements of the list on the stack;
then it executes the program you supplied, which at your option
may leave some results on the stack, or none, each time it executes.

Then DOSUBS puts N elements of the list on the stack again, but this
time starting with the second element of the original list; this
creates a sort of "moving frame" which always contains N elements
at a time, but advances by one position with each iteration.

DOSUBS stops as soon as a frame ending with the last element of
the original list has been processed.

After all list frames have been presented once each to your program,
DOSUBS finally gathers up all the results you left on the stack into
one final list, with the exception that if you left no results at all
on the stack (as determined by whether or not the stack grew in length),
then *no* final list is returned (rather than an empty list).

Two special additional commands are available to your program
when it is executed by DOSUBS:

The NSUB command returns the index number of the first element in
the current frame; this starts at 1 and then increases by 1 each
succeeding time your program is executed.

The ENDSUB command always returns the index number which will be
the index of the last frame; you can compare the results of
NSUB and ENDSUB to determine when you have reached the last frame.

The NSUB and ENDSUB commands generate errors when executed
outside of a DOSUBS environment.

DOLIST and DOSUBS are nearly identical when N=1, except that DOSUBS
offers you the opportunity to use the NSUB and ENDSUB commands
in the program that you apply to the list elements.

DOSUBS examples:

"Table of Differences" [same as G/GX DeltaList command]:
{ 0 1 4 9 16 25 36 } 2 \<< SWAP - \>> DOSUBS ==> { 1 3 5 7 9 11 }

"Moving Average":
{ 1 2 3 4 7 11 } 4 \<< + + + 4 / \>> DOSUBS ==> { 2.5  4  6.25 }

"Group by pairs":
{ 1 2 3 4 5 6 } 1 \<< NSUB 2 MOD NOT { 2 \->LIST } IFT \>> DOSUBS
     ==> { { 1 2 } { 3 4 } { 5 6 } }

Simulate G/GX STREAM command
(combine each previous result with new element, until list is used up):
{ 4 3 2 } 1 \<< NSUB 1 > { ^ } IFT \>> DOSUBS EVAL ==> 4096

---------------------------------------------------------------------

%%HP: T(3); @ DOLIST and DOSUBS for HP48S/SX

\<< @ Execute this program to create the set of programs
    @ DOLIST, DOSUBS, NSUB, ENDSUB in the current directory.

    @ The "?" character is alpha left-shift backspace.

  \<< OVER TYPE 5 == { 1 SWAP } IFT SWAP 0 RND
    SWAP OVER 2 + ROLLD \->LIST DUP SIZE DEPTH 3 -
    \-> ?f ?l ?n ?d \<< 1 ?l 1 GET SIZE
      FOR ?i 1 ?n
	FOR ?j ?l ?j GET ?i GET
	NEXT ?f EVAL
      NEXT DEPTH ?d - \->LIST \>>
      DUP SIZE NOT { DROP } IFT \>> 'DOLIST' STO

  \<< OVER TYPE 5 == { 1 SWAP } IFT SWAP 0 RND
    1 - 3 PICK SIZE OVER - DEPTH 4 -
    \-> ?l ?f ?n ?e ?d \<< 1 ?e
      FOR ?i 0 ?n
	FOR ?j ?l ?i ?j + GET
	NEXT ?f EVAL
      NEXT DEPTH ?d - \->LIST \>>
      DUP SIZE NOT { DROP } IFT \>> 'DOSUBS' STO

  0 \-> ?i \<< '?i'   'NSUB' STO \>> @ Local variable '?i'

  0 \-> ?e \<< '?e' 'ENDSUB' STO \>> @ Local variable '?e'

    @ You may purge this program after it stores programs
\>> @ DOLIST, DOSUBS, NSUB, ENDSUB in the current directory.


Notes:

o The "program" which you supply to DOLIST or DOSUBS may be either
  an actual program or the name of some stored program.  Our version
  will actually evaluate any object at all, but the G/GX won't,
  which should be kept in mind for compatibility.

o The G/GX permits the "count" argument N to be omitted in unusual cases,
  if it can determine exactly how many arguments the program requires;
  since most of the time DOLIST and DOSUBS are used with a count of 1,
  we assume a count of 1 if the argument N is omitted.

  To guarantee compatibility with the G/GX, we suggest that you
  always explicitly specify the value N.

o The G/GX does not accept empty list arguments; neither will our
  programs, but we didn't bother testing and giving an error for this.

  Programs which "adjust" DOLIST and/or DOSUBS to accept empty lists
  *and* also *return* an empty list (when you produce no results,
  which is certainly the case given empty input list[s])
  were posted on 1996/05/27 "Normalizing DOLIST & DOSUBS"
  (a follow-up note was posted on 1996/05/28).

o Pure User-RPL programs containing DOLIST and DOSUBS should be
  transportable in ascii mode between the S/SX and G/GX; the
  sizes and checksums may vary, however, because the various
  function words are commands (XLIB/ROMPTR) in the G/GX,
  but are global names in the S/SX.

o We created "programs" NSUB and ENDSUB for you, which each contain
  a "local" (type 7) variable name; you *must*not*edit* either of
  these two programs, or else the stored object will turn into
  a "global" (type 6) name, which will no longer work.

o We used unusual local variable names to avoid conflict with
  any other symbolic local names which might be included in
  stack objects (e.g. other programs or algebraic expressions)
  which may be passed for evaluation to either DOLIST or DOSUBS.

  There is no conflict when a program uses its own local names
  only within itself, but if it passes these names to another
  program which also defines the same name, a conflict occurs,
  as was posted 1996/10/20 "Local variable name conflicts!"

Search for old posts at <http://www.dejanews.com/home_sf.shtml>
(specify newsgroup name, subject word[s] and/or date range).

Subject search example: "local and variable and conflict*"

Happy New Year, and may all your lists be do-able.

-----------------------------------------------------------
With best wishes from:   John H Meyers   <jhmeyers@mum.edu>