Post Reply 
48: Moving average with DOSUBS
06-12-2015, 01:27 PM
Post: #1
48: Moving average with DOSUBS
I need to whip up a quick moving-average program. This doesn't work:

Code:
AVG:
\<< DUP \GSLIST SWAP SIZE / \>>

MAVG:
\<< \-> L N
  \<< L N \<< N \->LIST AVG \>> DOSUBS \>>
\>>

And it's fairly obvious why: DOSUBS doesn't like the local name 'N' being present in the program it's being passed.

What's the trick to modifying a program on the stack? I figure I should be able to recall the value of N, prepend it to \<< ->LIST AVG \>>, and have a program that DOSUBS will take. You can't simply concatenate programs with +, and while it works with lists, DOSUBS won't accept the function stored as a list.
Visit this user's website Find all posts by this user
Quote this message in a reply
06-12-2015, 02:55 PM
Post: #2
RE: 48: Moving average with DOSUBS
? But it does work, and DOSUBS has no issues with local variables being in the executable object..
{ 1 2 3 } 2 MAVG returns { 1.5 2.5 } as it should
BTW you don't need to include L in your local environment

Code:
MAVG:
\<< \-> N
  \<< N \<< N \->LIST AVG \>> DOSUBS \>>
\>>

Cheers, Werner
Find all posts by this user
Quote this message in a reply
06-12-2015, 03:03 PM
Post: #3
RE: 48: Moving average with DOSUBS
(06-12-2015 02:55 PM)Werner Wrote:  ? But it does work, and DOSUBS has no issues with local variables being in the executable object..
{ 1 2 3 } 2 MAVG returns { 1.5 2.5 } as it should
BTW you don't need to include L in your local environment

Code:
MAVG:
\<< \-> N
  \<< N \<< N \->LIST AVG \>> DOSUBS \>>
\>>

Cheers, Werner

Alright, that's weird... At first, I was getting "DOSUBS Error: Invalid User Function". Not sure what bizarre typo was leading to that.

Anyway, if anybody knows how to modify programs and convert them to/from lists, I'd still find that useful.
Visit this user's website Find all posts by this user
Quote this message in a reply
06-12-2015, 03:26 PM (This post was last modified: 06-12-2015 03:51 PM by John R. Graham.)
Post: #4
RE: 48: Moving average with DOSUBS
(06-12-2015 03:03 PM)Dave Britten Wrote:  Anyway, if anybody knows how to modify programs and convert them to/from lists, I'd still find that useful.
Convert the program to a string with →STR, modify to your heart's content, and then convert back to a program with OBJ→. This has the advantage that it forces a recompile of the program so that your changes are guaranteed to take effect.

Edit: I think I'm mis-remembering the compiling thing, which only applies to algebraics.

- John
Find all posts by this user
Quote this message in a reply
06-12-2015, 04:03 PM
Post: #5
RE: 48: Moving average with DOSUBS
(06-12-2015 03:26 PM)John R. Graham Wrote:  
(06-12-2015 03:03 PM)Dave Britten Wrote:  Anyway, if anybody knows how to modify programs and convert them to/from lists, I'd still find that useful.
Convert the program to a string with →STR, modify to your heart's content, and then convert back to a program with OBJ→. This has the advantage that it forces a recompile of the program so that your changes are guaranteed to take effect.

Edit: I think I'm mis-remembering the compiling thing, which only applies to algebraics.

- John

That should work in most cases, including this one, but if the program has any non-UserRPL stuff in it, e.g. takeovers, externals, etc., then recompiling it from the string representation will probably either fail with an error, or store a broken program.

I could swear I saw some method to build/modify a program on the fly without doing any string conversions. Can't remember where I saw it, though. It may have been an algebraic-to-RPN converter, unless I'm imagining the existence of that too.
Visit this user's website Find all posts by this user
Quote this message in a reply
06-12-2015, 04:31 PM
Post: #6
RE: 48: Moving average with DOSUBS
Interesting. If you (re-)locate such a feature, I'd love to hear about it.

- John
Find all posts by this user
Quote this message in a reply
06-12-2015, 05:50 PM
Post: #7
RE: 48: Moving average with DOSUBS
By converting programs to lists and back again, are you referring to the →LST and →PRG command in library 256? If you execute 256.02 MENU, you'll see →LST on the F5 key. Pressing it with a program on the stack will turn it into a list, which can easily be exploded to the stack via OBJ→, or manipulated like any other list. Then press NXT to see →PRG on the F1 key. It takes any list and turns it into a program.

If that's not what you were looking for, ignore this posting.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
06-12-2015, 06:09 PM
Post: #8
RE: 48: Moving average with DOSUBS
Wow. Those descriptions are so terse in the AUR that I hadn't realized what they did. Thank you.

- John
Find all posts by this user
Quote this message in a reply
06-12-2015, 06:23 PM
Post: #9
RE: 48: Moving average with DOSUBS
(06-12-2015 05:50 PM)Joe Horn Wrote:  By converting programs to lists and back again, are you referring to the →LST and →PRG command in library 256? If you execute 256.02 MENU, you'll see →LST on the F5 key. Pressing it with a program on the stack will turn it into a list, which can easily be exploded to the stack via OBJ→, or manipulated like any other list. Then press NXT to see →PRG on the F1 key. It takes any list and turns it into a program.

If that's not what you were looking for, ignore this posting.

That sounds like what I was thinking of.

Now, how do I do that on a 48, which doesn't have menu 256?
Visit this user's website Find all posts by this user
Quote this message in a reply
06-12-2015, 09:56 PM (This post was last modified: 06-12-2015 09:57 PM by Joe Horn.)
Post: #10
RE: 48: Moving average with DOSUBS
(06-12-2015 06:23 PM)Dave Britten Wrote:  That sounds like what I was thinking of. Now, how do I do that on a 48, which doesn't have menu 256?

Gotta use System RPL, but that can be faked with SYSEVAL in a User RPL program. Delightfully, the SRPL command CHANGETYPE has remained stable at ROM address #05AB3h in every RPL model, from 48SX version A, through the 48GX version R, through the 50g version 2.15. So the following little routine will convert any program into a list or vice versa, and leave any other input unchanged. It's non-optimized for ease of human reading... if such a thing is possible in RPL. Be sure to get the hex numbers exactly right, or it'll do Bad Things.

'P~L' (Program/List Toggler) for HP 48/49/50, all versions
<< CASE
DUP TYPE 8 ==
THEN #2A74h #5A03h SYSEVAL #5AB3h SYSEVAL END
DUP TYPE 5 ==
THEN #2D9Dh #5A03h SYSEVAL #5AB3h SYSEVAL END
END >>

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
06-12-2015, 10:54 PM
Post: #11
RE: 48: Moving average with DOSUBS
(06-12-2015 09:56 PM)Joe Horn Wrote:  
(06-12-2015 06:23 PM)Dave Britten Wrote:  That sounds like what I was thinking of. Now, how do I do that on a 48, which doesn't have menu 256?

Gotta use System RPL, but that can be faked with SYSEVAL in a User RPL program. Delightfully, the SRPL command CHANGETYPE has remained stable at ROM address #05AB3h in every RPL model, from 48SX version A, through the 48GX version R, through the 50g version 2.15. So the following little routine will convert any program into a list or vice versa, and leave any other input unchanged. It's non-optimized for ease of human reading... if such a thing is possible in RPL. Be sure to get the hex numbers exactly right, or it'll do Bad Things.

'P~L' (Program/List Toggler) for HP 48/49/50, all versions
<< CASE
DUP TYPE 8 ==
THEN #2A74h #5A03h SYSEVAL #5AB3h SYSEVAL END
DUP TYPE 5 ==
THEN #2D9Dh #5A03h SYSEVAL #5AB3h SYSEVAL END
END >>

Sweet, thanks. I figured there might be a couple of SYSEVALs that would do that.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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