Post Reply 
HP 50g List function question
09-09-2017, 10:47 PM
Post: #1
HP 50g List function question
Hello everyone,

Does anyone know if there's a function that will turn a List into a list of accumulated numbers as in {a, b, c} to {a, a+b, a+b+c}. For example,

{2, 7, 1, 9} to {2, 9, 10, 19}

or

{7, 1, 3, 6, 3} to {7, 8, 11, 17, 20}

If not, is there a function that can eliminate the first or last 'n' terms on a List or separate a list in two (one of size 'n').

Thanks in advance,
Sincerely,

Andres
Find all posts by this user
Quote this message in a reply
09-10-2017, 07:18 AM (This post was last modified: 09-10-2017 07:38 AM by pier4r.)
Post: #2
RE: HP 50g List function question
for the first problem, I am not sure if there is a built in solution or there is some neat solution somewhere in some library.

For the moment I can offer this:
(you need to type it either in the 50g or copy it in notepad and then copy it over through the connectivity kit)
Remember that symbols like "\<<" are the conversion of special characters in the hp font. See hp 50g Advanced user manual page J-2 (is in the appendix)
Code:

@www.hpmuseum.org/forum/thread-9028.html
    @ from { 1 2 3 4 } to { 1 3 6 10 }
    \<<
      0 "result" DROP
      
      \->
      @input
      inputList
    
      @local var
      result
    
      \<<
      
        @go through every element of the list
        @and increase the sum, leaving the current sum on the stack
        inputList
        1
        \<<
          'result' STO+
          result
        \>>
        DOSUBS
      \>>
    \>>


Also suggested:
- http://www.hpmuseum.org/forum/thread-8209.html
- http://www.hpmuseum.org/forum/thread-8555.html


For the second question you can use the ListExt from DavidM. (http://www.hpmuseum.org/forum/thread-8555.html , see last posts, the first post is not updated)

There is a command called LRMOV in the listExt library, but it removes the first 'n' objects, for the last 'n' you need to use a combination of commands (would be nice if LRMOV from the last would be added, hint hint).

Anyway there are commands to pick the first n or last n entries in the list.

So you have:
Code:

\<<
  @program to remove the first n entries

  \->
  inputList
  elementsToDelete
  
  \<<
    @we put the list on the stack for later
    inputList
  
    @it computes the size of the list
    inputList SIZE
    
    @on the stack there are
    @2: list
    @1: list size
    
    @we check now how many entries we should keep
    @ "size-n"
    elementsToDelete -
    
    @on the stack there are
    @2: list
    @1: size-n elements to keep
    LLAST
    
    @returns only size-n last elements
  \>>  
\>>


\<<
  @program to remove the last n entries

  \->
  inputList
  elementsToDelete
  
  \<<
    @we put the list on the stack for later
    inputList
  
    @it computes the size of the list
    inputList SIZE
    
    @on the stack there are
    @2: list
    @1: list size
    
    @we check now how many entries we should keep
    @ "size-n"
    elementsToDelete -
    
    @on the stack there are
    @2: list
    @1: size-n elements to keep
    LFRST
    
    @returns only size-n first elements
  \>>  
\>>

Wikis are great, Contribute :)
Find all posts by this user
Quote this message in a reply
09-10-2017, 07:21 AM (This post was last modified: 09-10-2017 08:32 AM by Gilles59.)
Post: #3
RE: HP 50g List function question
(09-09-2017 10:47 PM)Andres1 Wrote:  {a, b, c} to {a, a+b, a+b+c}.

Hi, with Gopher List Library :

Code:
  « + » Scanl1

without external Library :

Code:
 1. 
 «
  CASE    
   NSUB 1. == THEN DUP END    @ special case first item : no +
   NSUB ENDSUB == THEN + END  @ special case last item : no DUP
   + DUP                      @ All other cases
  END
 »
DOSUBS
Find all posts by this user
Quote this message in a reply
09-10-2017, 07:46 AM
Post: #4
RE: HP 50g List function question
goferlist

http://www.hpcalc.org/details/6529

how to install a library
http://www.hpcalc.org/install.php

(follow the info of the 49g)

in short: save the library in your home.
move it (with the filer for example), to the flash storage
warm restart your 50g. (holding on and while holding pressing f3 , see hp50g user guide page G-3 (appendix) )

Then after the restart press right shift (the orange one) and then 2 (where LIB is written), there you can see your libraries on the soft menu.

Wikis are great, Contribute :)
Find all posts by this user
Quote this message in a reply
09-10-2017, 08:23 AM
Post: #5
RE: HP 50g List function question
(09-10-2017 07:18 AM)pier4r Wrote:  For the second question you can use the ListExt from DavidM. (http://www.hpmuseum.org/forum/thread-8555.html , see last posts, the first post is not updated)

There is a command called LRMOV in the listExt library, but it removes the first 'n' objects, for the last 'n' you need to use a combination of commands (would be nice if LRMOV from the last would be added, hint hint).

Hi Pier... Where is the last version of ListExt with LRMOV ? I dont find it.

I suppose that REVLIST LRMOV REVLIST will do the job

Quote: (would be nice if LRMOV from the last would be added, hint hint).

Something like for example -3 LRMOV
Find all posts by this user
Quote this message in a reply
09-10-2017, 08:48 AM (This post was last modified: 09-10-2017 09:00 AM by Gilles59.)
Post: #6
RE: HP 50g List function question
(09-09-2017 10:47 PM)Andres1 Wrote:  Hello everyone,


If not, is there a function that can (...)separate a list in two (one of size 'n').

Thanks in advance,
Sincerely,

Andres

With GoferList again (see Pier post to download the Library) :

Code:

{ a b c d e f h g } 3 Split

-> 

{ a b c }
{ d e f g h }

Without :

Code:

{ a b c d e f h g } 3
«
 DUP2 1 SWAP SUB 
 UNROT 1 + OVER SIZE SUB
»
Find all posts by this user
Quote this message in a reply
09-10-2017, 09:40 AM
Post: #7
RE: HP 50g List function question
(09-10-2017 08:23 AM)Gilles59 Wrote:  Hi Pier... Where is the last version of ListExt with LRMOV ? I dont find it.

Yup if the last update is not in the first post (or somewhere stable) it is difficult to find. Anyway the last update of the listExt so far is here:
http://www.hpmuseum.org/forum/thread-855...l#pid78160

Wikis are great, Contribute :)
Find all posts by this user
Quote this message in a reply
09-10-2017, 01:36 PM
Post: #8
RE: HP 50g List function question
(09-10-2017 07:21 AM)Gilles59 Wrote:  
(09-09-2017 10:47 PM)Andres1 Wrote:  {a, b, c} to {a, a+b, a+b+c}.

Hi, with Gopher List Library :

Code:
  « + » Scanl1

without external Library :

Code:
 1. 
 «
  CASE    
   NSUB 1. == THEN DUP END    @ special case first item : no +
   NSUB ENDSUB == THEN + END  @ special case last item : no DUP
   + DUP                      @ All other cases
  END
 »
DOSUBS

A nice, elegant version by Jeremy Hawdon, from Datafile Vol. 27 No.6:

Code:
\<< 0 SWAP 1.
   \<< OVER +
   \>> DOLIST NIP
\>>

Really, though, anyone doing this sort of programming needs to have GoferLists and ListExt.

John
Find all posts by this user
Quote this message in a reply
09-10-2017, 03:28 PM
Post: #9
RE: HP 50g List function question
My attempt at this ended up being the same as Jeremy Hawdon's from John Keith's post. But the GoferList version is going to be hard to beat Smile.

As for the secondary question (deleting n elements from end/splitting a list):

I wouldn't use LRMOV for this, as it is designed with a different purpose in mind (deleting arbitrary, possibly non-contiguous elements as identified in a list argument).

If you were to use the ListExt functions, there's several ways to approach it. Which one to use probably depends on the source of "n" and/or the intended use of the result.

Probably the easiest is to use one of the newer commands (LFRST). If n is defined as the number of leading elements to keep:
Code:
{ 1 2 3 4 5 6 } 4 LFRST => { 1 2 3 4 }

Another of the newer commands (LRSPL) can be used if you know the complementary number (n becomes the number of trailing elements to delete). It splits the given list into two by setting the split point counting from the right:
Code:
{ 1 2 3 4 5 6 } 2 LRSPL => { { 1 2 3 4 } { 5 6 } }

...then you would simply need to GET the first sublist. So combining the two operations together becomes:
Code:
{ 1 2 3 4 5 6 } 2 LRSPL 1 GET => { 1 2 3 4 }

If what you really wanted was simply "n" trailing elements, use the following:
Code:
{ 1 2 3 4 5 6 } 2 LLAST => { 5 6 }
Find all posts by this user
Quote this message in a reply
09-10-2017, 03:56 PM
Post: #10
RE: HP 50g List function question
Thanks so much everyone, I didn't expect such fast replies. I ended up using Jeremy Hawdon's version as it is very elegant and I only needed that for now, but I'll be sure to check out the GoferList and ListExt if I need more functions.

Thanks again,

Andres
Find all posts by this user
Quote this message in a reply
09-10-2017, 07:41 PM
Post: #11
RE: HP 50g List function question
For the first question:

«
1 « NSUB 1 > {OVER +} IFT » DOSUBS
»
Find all posts by this user
Quote this message in a reply
09-10-2017, 07:48 PM
Post: #12
RE: HP 50g List function question
(09-10-2017 07:41 PM)Juan14 Wrote:  For the first question:

«
1 « NSUB 1 > {OVER +} IFT » DOSUBS
»

Well done !
Find all posts by this user
Quote this message in a reply
Post Reply 




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