HP Forums

Full Version: hp 28s - creating nested lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
? It works for me.
When you say the code has errors, does it not compile or does it not run?
Does the 28 have a FOR .. STEP statement? If not, then you can't use this version and will have to stick to one of the two others.
(I don't have a 28)
If it compiles, then you must've entered a typo..

NDUPN is a useful little subprogram.. I always try to make 'beautiful' code, ie. short yet clear, and this way it's both ;-) And perhaps NDUPN can be used elsewhere?

Cheers, Werner
It compiles, but as written it gives an error message about a bad 'FOR statement.' I checked for typos.

The reference manual shows the HP 28 FOR/STEP statement as:

start finish FOR name loop-clause increment STEP

increment must be a real number.

For example:
Code:
1 11 FOR x x SQ 2 STEP
places the squares of the integers 1, 3, 5, 7, 9, and 11 on the stack.
Aha.. that's probably it.
The 48/49 STEP syntax specifies:
Quote:If the argument of STEP is an algebraic expression or a name, it is automatically evaluated to a number.
But the 28 accepts only a real number, not a variable holding one.
So, just add EVAL before STEP (or even better, before DUP). That should do it.
Code:
\<<
  \-> n
  \<<
    1 2 n
    FOR i
      DUPN i EVAL DUP
    STEP
    n - DUP 0 <
    { NEG DUPN }
    { DROPN }
    IFTE
    n
  \>>
\>>

Cheers, Werner
Edit: The True/False objects in the IFTE statement should be program objects on the 28.
Code:
\<<
  \-> n
  \<<
    1 2 n
    FOR i
      DUPN i EVAL DUP
    STEP
    n - DUP 0 <
    \<< NEG DUPN \>>
    \<< DROPN \>>
    IFTE
    n
  \>>
\>>
Then you might as well write it as a normal IF THEN ELSE statement

Code:
IF DUP 0 <
THEN NEG DUPN
ELSE DROPN
END

7.5 bytes shorter
Pages: 1 2
Reference URL's