Post Reply 
RPL equivalents to BREAK, CYCLE, EXIT, etc.?
06-08-2014, 08:22 PM
Post: #15
RE: RPL equivalents to BREAK, CYCLE, EXIT, etc.?
(06-08-2014 06:05 PM)Thomas Klemm Wrote:  How would you make it more readable?

I actually think this is a good example. In your code, you've used a definite loop for something that isn't actually definite, in that you don't know heading into it how many iterations it may actually need.

If I understood the logic of your code, I believe the following captures the gist of what you did, but in a different way:
Code:

\<<
   2 OVER \v/ 0 \-> n test last hasDivisor
   \<<
      DO
         n test MOD NOT 'hasDivisor' STO
         test 1 + 'test' STO
      UNTIL
         test last >
         hasDivisor
         OR
      END
      hasDivisor NOT
   \>>
\>>

I've intentionally not tried to optimize this for stack use, short variable names, etc. because I believe the goal was readability (and restructuring) as opposed to code size or speed.

While this is more readable to me, it may not be for everyone. The original program you posted was slightly less clear to me for these reasons:

- It used a definite loop structure for something that wasn't really intended to be definite (you don't have an expectation that all iterations are actually needed)
- It used IFERR for something that's not really an exception, but rather the more typical situation where you actually find a factor

As such, I had to spend a little time making sure I understood what the real intent was behind the steps. The code snippet I used above is (IMHO) more in keeping with the basic intent of the logic flow -- "keep trying until either of two conditions is met". It seems to me your logic flow was more like "do this a set number of times, unless you happen upon a factor".

Does this make sense?
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: RPL equivalents to BREAK, CYCLE, EXIT, etc.? - DavidM - 06-08-2014 08:22 PM



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