From: kskalb@immd1.informatik.uni-erlangen.de (Klaus Kalb)
Subject: Utility: applylist
Summary: APPLYLIST: applies a program to each entry of a list
Organization: CSD., University of Erlangen
Date: Thu, 10 Dec 1992 20:02:37 GMT
Keywords: list apply loop hp48 sysrpl


Hello,

This is a little utility I find very useful and I want to share it.
I have submitted it to comp.sources.hp48 as well, but since it will
take some time to appear over there, I decided to post it as well.

Enjoy, -KK

BEGIN_RDME applylist.rdm

Name:		APPLYLIST
Description:	applies a program to each entry of a list
Comment:	written in SysRPL
Version:	1.0
Size:		92.5
CRC:		8A35

END_RDME applylist.rdm

BEGIN_DOC applylist.doc

There is one thing I always missed from UserRPL: The possiblity to 
iterate a program over all entries in  a list.
Something like
	FOR obj IN list DO ... END
would sometimes be handy. So I wrote a utility the offers a workaround.

APPLYLIST takes to arguments:
	A list in level 2, lets assume it's L = { O1 O2 ... On }
	Anything in level 1, lets call this P
and it does
	- Bind L,P and the current depth to local vars (unnamed ones)
	- Push O1 to the stack, execute P
	- Push O2 to the stack, execute P
	...
	- Push On to the stack, execute P
	- Make all new objects on the stack into a list

Examples:

---	Double all numbers in a list
	{ 1 2 3 } 
	\<< 2 * \>> 
	APPLYLIST		-->	{ 2 4 6 }

---	Double all numbers in a list, in a somewhat different sense
	{ 1 2 3 } 
	\<< DUP \>> 
	APPLYLIST		-->	{ 1 1 2 2 3 3 }

---	Delete all zeros from a list
	{ 0 1 2 0 3 4 }
	\<< IF DUP 0 SAME THEN DROP END \>>	
	APPLYLIST		--> { 1 2 3 4 }
 
---	Add up all numbers in the list
	0
	{ 1 2 3 4 }
	\<< + \>>
	APPLYLIST 		-->	10 { }

---	Find the amount of mem used in port 0
	0 DUP PVARS DROP \<< RCL BYTES SWAP DROP + \>> APPLYLIST DROP 

---     Recall all vars to the stack and tag them with their name:
	VARS \<< DUP RCL SWAP \->TAG \>> APPLYLIST DROP

And many more...

Disclaimer: 
  Any coincidences of the program and the documentation are casually
  and were not indented. Of course there are situations in which
  running this program will destroy your HP48. ;-) 

Mail any comments to:

	kalb@informatik.uni-erlangen.de


END_DOC applylist.doc

BEGIN_SRC applylist.src
::
  CK2NOLASTWD
  CK&DISPATCH1
  # 50					(*  2:list 1:any *)
  ::
    DEPTH #2-				(*  get depth *)
    THREE ' NULLLAM CACHE		(*  bind args *)
    4GETLAM LENCOMP 			(*  get size of list *)
    DUP#0= ITE_DROP			(*  check for empty list *)
    :: #1+_ONE_DO			
      4GETLAM INDEX@ NTHCOMPDROP	(* get Nth element *)
      3GETLAM 				(* get prog *)
      EVAL			
    LOOP ;
    DEPTH 2GETLAM 2DUP#<		(*  check depth *)
    ITE					(*  any new objects ? *)
      2DROP				(*  objects disappeared, no list *)
      :: #- {}N ;			(*  create list *)
    ABND			
  ;
;



END_SRC applylist.src

BEGIN_UU applylist.uue
begin 640 applylist.bin
M2%!(4#0X+46=+="H&+*/$9$"4 #0V0),,>#D PU <.D&,$V3SF$X%+9G!68B
MAJ5AG2VP/0<X%!8B!YPKYD!ACF] ,P<K,< 4 ^<3MHEBV!J&)0.=+0#> UE4
+L!(#EW2P$@,K,0 B
 
end
END_UU applylist.uue

BEGIN_ASC applylist.asc
%%HP: T(1)A(R)F(.);
"D9D20D8A812BF811192005000D9D20C4130E4E30D004079E6003D439EC168341
6B76506622685A16D9D20BD3708341612270C9B26E0416E8F6043370B2130C41
307E316B98268DA1685230D9D200ED3095450B213079470B2130B213053A8"
END_ASC applylist.asc

---
------------------------------------------------------------------------------
    Klaus Kalb     | mail :  IMMD1 / Martenstr. 3 / W-8520 Erlangen / Germany 
 Werch ein Illtum  | email:  kskalb@immd1.informatik.uni-erlangen.de   
------------------------------------------------------------------------------