home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / hp48 / 6259 < prev    next >
Encoding:
Text File  |  1992-12-11  |  3.8 KB  |  143 lines

  1. Newsgroups: comp.sys.hp48
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!usc!enterpoop.mit.edu!ira.uka.de!fauern!fauna!immd1.informatik.uni-erlangen.de!kskalb
  3. From: kskalb@immd1.informatik.uni-erlangen.de (Klaus Kalb)
  4. Subject: Utility: applylist
  5. Message-ID: <Bz27oE.7B1@immd4.informatik.uni-erlangen.de>
  6. Summary: APPLYLIST: applies a program to each entry of a list
  7. Sender: news@immd4.informatik.uni-erlangen.de
  8. Organization: CSD., University of Erlangen
  9. Date: Thu, 10 Dec 1992 20:02:37 GMT
  10. Keywords: list apply loop hp48 sysrpl
  11. Lines: 130
  12.  
  13.  
  14. Hello,
  15.  
  16. This is a little utility I find very useful and I want to share it.
  17. I have submitted it to comp.sources.hp48 as well, but since it will
  18. take some time to appear over there, I decided to post it as well.
  19.  
  20. Enjoy, -KK
  21.  
  22. BEGIN_RDME applylist.rdm
  23.  
  24. Name:        APPLYLIST
  25. Description:    applies a program to each entry of a list
  26. Comment:    written in SysRPL
  27. Version:    1.0
  28. Size:        92.5
  29. CRC:        8A35
  30.  
  31. END_RDME applylist.rdm
  32.  
  33. BEGIN_DOC applylist.doc
  34.  
  35. There is one thing I always missed from UserRPL: The possiblity to 
  36. iterate a program over all entries in  a list.
  37. Something like
  38.     FOR obj IN list DO ... END
  39. would sometimes be handy. So I wrote a utility the offers a workaround.
  40.  
  41. APPLYLIST takes to arguments:
  42.     A list in level 2, lets assume it's L = { O1 O2 ... On }
  43.     Anything in level 1, lets call this P
  44. and it does
  45.     - Bind L,P and the current depth to local vars (unnamed ones)
  46.     - Push O1 to the stack, execute P
  47.     - Push O2 to the stack, execute P
  48.     ...
  49.     - Push On to the stack, execute P
  50.     - Make all new objects on the stack into a list
  51.  
  52. Examples:
  53.  
  54. ---    Double all numbers in a list
  55.     { 1 2 3 } 
  56.     \<< 2 * \>> 
  57.     APPLYLIST        -->    { 2 4 6 }
  58.  
  59. ---    Double all numbers in a list, in a somewhat different sense
  60.     { 1 2 3 } 
  61.     \<< DUP \>> 
  62.     APPLYLIST        -->    { 1 1 2 2 3 3 }
  63.  
  64. ---    Delete all zeros from a list
  65.     { 0 1 2 0 3 4 }
  66.     \<< IF DUP 0 SAME THEN DROP END \>>    
  67.     APPLYLIST        --> { 1 2 3 4 }
  68.  
  69. ---    Add up all numbers in the list
  70.     0
  71.     { 1 2 3 4 }
  72.     \<< + \>>
  73.     APPLYLIST         -->    10 { }
  74.  
  75. ---    Find the amount of mem used in port 0
  76.     0 DUP PVARS DROP \<< RCL BYTES SWAP DROP + \>> APPLYLIST DROP 
  77.  
  78. ---     Recall all vars to the stack and tag them with their name:
  79.     VARS \<< DUP RCL SWAP \->TAG \>> APPLYLIST DROP
  80.  
  81. And many more...
  82.  
  83. Disclaimer: 
  84.   Any coincidences of the program and the documentation are casually
  85.   and were not indented. Of course there are situations in which
  86.   running this program will destroy your HP48. ;-) 
  87.  
  88. Mail any comments to:
  89.  
  90.     kalb@informatik.uni-erlangen.de
  91.  
  92.  
  93. END_DOC applylist.doc
  94.  
  95. BEGIN_SRC applylist.src
  96. ::
  97.   CK2NOLASTWD
  98.   CK&DISPATCH1
  99.   # 50                    (*  2:list 1:any *)
  100.   ::
  101.     DEPTH #2-                (*  get depth *)
  102.     THREE ' NULLLAM CACHE        (*  bind args *)
  103.     4GETLAM LENCOMP             (*  get size of list *)
  104.     DUP#0= ITE_DROP            (*  check for empty list *)
  105.     :: #1+_ONE_DO            
  106.       4GETLAM INDEX@ NTHCOMPDROP    (* get Nth element *)
  107.       3GETLAM                 (* get prog *)
  108.       EVAL            
  109.     LOOP ;
  110.     DEPTH 2GETLAM 2DUP#<        (*  check depth *)
  111.     ITE                    (*  any new objects ? *)
  112.       2DROP                (*  objects disappeared, no list *)
  113.       :: #- {}N ;            (*  create list *)
  114.     ABND            
  115.   ;
  116. ;
  117.  
  118.  
  119.  
  120. END_SRC applylist.src
  121.  
  122. BEGIN_UU applylist.uue
  123. begin 640 applylist.bin
  124. M2%!(4#0X+46=+="H&+*/$9$"4 #0V0),,>#D PU <.D&,$V3SF$X%+9G!68B
  125. MAJ5AG2VP/0<X%!8B!YPKYD!ACF] ,P<K,< 4 ^<3MHEBV!J&)0.=+0#> UE4
  126. +L!(#EW2P$@,K,0 B
  127.  
  128. end
  129. END_UU applylist.uue
  130.  
  131. BEGIN_ASC applylist.asc
  132. %%HP: T(1)A(R)F(.);
  133. "D9D20D8A812BF811192005000D9D20C4130E4E30D004079E6003D439EC168341
  134. 6B76506622685A16D9D20BD3708341612270C9B26E0416E8F6043370B2130C41
  135. 307E316B98268DA1685230D9D200ED3095450B213079470B2130B213053A8"
  136. END_ASC applylist.asc
  137.  
  138. ---
  139. ------------------------------------------------------------------------------
  140.     Klaus Kalb     | mail :  IMMD1 / Martenstr. 3 / W-8520 Erlangen / Germany 
  141.  Werch ein Illtum  | email:  kskalb@immd1.informatik.uni-erlangen.de   
  142. ------------------------------------------------------------------------------
  143.