Post Reply 
HHC 2019 will be in Reno, Nevada, Sept 21-22
10-01-2019, 02:11 AM
Post: #62
RE: HHC 2019 will be in Reno, Nevada, Sept 21-22
(09-30-2019 09:54 PM)David Hayden Wrote:  Claudio, can you explain how this part of the code works? I'm guessing that RANGE produces a list of numbers from 0 to 9 by 1's. And I think NEWRPL's + operator adds corresponding elements in lists (rather than appending lists). But how does it create a list of 1000 numbers?

Thanks,
Dave

(09-30-2019 04:15 PM)Claudio L. Wrote:  
Code:

«
  0 9 1 RANGE      @ Make a case-list of numbers 0-9
 3 ^ DUPDUP + +    @ Sum of the cubes of the digits of ALL numbers from 0 to 999

Yes, sorry, I posted in a rush with no time to explain.
RANGE is similar to the Python Range(), creates a list of integers, giving start, end and step. In newRPL, it creates not a list but a case-list. It's a new type of object, similar to a list but with different overloaded operators, so it behaves different.
I call them case-lists because it's a list of alternative results. When you assign a case list c{ 1 2 3 } to a variable X, it means X may take any of the values in the list. For example the square root of 4 could return c{ 2 -2}, because it could take either value.
When you add 2 case-lists, the result is all possible combinations: c{1 2 } c{ 3 4 } + returns c{ 1+3 2+3 1+4 2+4} since there could be 4 different results.
Back to the code above, RANGE just makes the case-list with 0 to 9. 3 ^ simply cubes each element. DUPDUP + + results in a list with all possible combinations of the sum of the cubes. The resulting list has all 1000 numbers, and the order of those numbers happens to match the order of the numbers 0 to 999. So the index of the list NSUB-1 is also the 3-digit number that gave origin to each element.
Case-lists were added to newRPL to be able to process expressions where a function may have multiple results, or when you want to try several values for a variable on a single EVAL.
So far it's used by ALLROOTS, which returns all roots where XROOT returns only the principal value, but case-lists are really handy.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HHC 2019 will be in Reno, Nevada, Sept 21-22 - Claudio L. - 10-01-2019 02:11 AM



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