manual:chapter5:basics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
manual:chapter5:basics [2017/11/04 01:20]
pier4r formatting
manual:chapter5:basics [2021/09/15 10:24]
claudio
Line 8: Line 8:
 A typical sequence of a program is to place some objects on the stack, do one or more operations and leave the result in the stack. For example: A typical sequence of a program is to place some objects on the stack, do one or more operations and leave the result in the stack. For example:
  
-<code>« 1 2 + »</code>+<code> 
 +« 
 +  1 2 + 
 +»</code>
  
 Let's analyze the program above: Let's analyze the program above:
Line 18: Line 21:
 And that's the most basic RPL program. Operators and functions always take some values from the stack and leave some results on it. A program may include multiple operations: And that's the most basic RPL program. Operators and functions always take some values from the stack and leave some results on it. A program may include multiple operations:
  
-<code>« 1 2 + 2 / »</code>+<code>« 
 +  1 2 + 
 +  2 / 
 +»</code>
  
 The program above starts identical to the previous example, and after the + operator is executed, the resulting number 3 is left on the stack. As the program continues, the number 2 is put on level 1, relegating the number 3 to level 2. The division operator takes 2 numbers and performs the division of the number in level 2 divided by the number in level 1. In this case, level 2 had a 3 and level 1 had a 2, therefore the number 1.5 will be left on the stack as a result. The program above starts identical to the previous example, and after the + operator is executed, the resulting number 3 is left on the stack. As the program continues, the number 2 is put on level 1, relegating the number 3 to level 2. The division operator takes 2 numbers and performs the division of the number in level 2 divided by the number in level 1. In this case, level 2 had a 3 and level 1 had a 2, therefore the number 1.5 will be left on the stack as a result.
Line 24: Line 30:
 ---- ----
  
-==== Operators and Commands ====+==== Operators, Functions and Commands ==== 
 + 
 +In the examples above we showed how + and / would take some objects from the stack, operate on them and place the result in the stack. This is the basic operation of all operators and commands in RPL. All commands, functions and operators take some (or none) arguments from the stack, and leave some (or none) results in it. There are hundreds of RPL keywords, all based on the same principle of taking and leaving objects on the stack. Future chapters will describe each keyword in depth, for now only a few ones will be introduced that define the basic core of the RPL language.  
 + 
 +Fundamentally all the keywords can be divided into 4 classes: commands, operators, functions and analytic functions. Difference between classes isn't in the effect they have on the stack (as said above, almost any keyword manipulates the stack) rather in behavior and scope. 
 + 
 +=== Commands === 
 + 
 +Commands perform actions. They are used to control program flow (''IF'', ''WHILE'', ''FOR''), manipulate objects (''PUT'', ''HEAD'') or variables (''STO+'', ''QUOTEID'') and setting the system (''SETLOCALE'', ''ASNKEYS''). 
 + 
 +=== Operators === 
 + 
 +Operators manipulate objects, returning results. Usually these objects are [[manual:chapter3:reals|real numbers]], but they can also be [[manual:chapter3:angles|angles]], [[manual:chapter3:matrix|matrices]] or [[manual:chapter3:symbolic|symbolic expressions]]. 
 + 
 +Most operators are //binary// i.e. they need two arguments and when used within symbolic expressions are distinguished by the fact that they adopt the //infix// notation. There also are a few //unary// operators adopting the //prefix// notation in symbolics (unary ''+'' and ''-'') and also one operator adopting //postfix// notation (''!'', the factorial operator). 
 + 
 +The basic arithmetic operators work as expected (''+'' and ''/'' were already introduced), taking objects from the stack and leaving the result of the operation. Test operators (''>'', ''<'', ''≤'', ''≥'', ''=='' ) take two objects from the stack, perform a comparison and leave a 1 on the stack if the test was true, and a 0 if the test failed. The true condition is always represented with the number 1, while the false condition is always represented with the number 0. 
 + 
 +=== Functions === 
 + 
 +Functions, like operators, manipulate objects and return results. They can be inserted into symbolic expressions, adopting a //functional// notation (i.e. their name is followed by a list of parameters enclosed by parentheses) but differ from analytic functions by the fact that they cannot be differentiated or integrated, either because they returns non numerical objects (''PHERMITE'', ''PMUL'') or because they are not continuous and/or differentiable (''ISPRIME?'', ''RAND'', ''MIN'').
  
-In the examples above we showed how + and / would take some objects from the stack, operate on them and place the result in the stack. This is the basic operation of all operators and commands in RPL. All commands and operators take some (or none) arguments from the stack, and leave some (or none) results in it. There are hundreds of RPL commands, all based on the same principle of taking and leaving objects on the stack. Future chapters will describe each command in depth, for now only a few commands will be introduced that define the basic core of the RPL language. The basic arithmetic operators work as expected (+ and / were already introduced), taking objects from the stack and leaving the result of the operation. Test operators (>, <, ≤, ≥, == ) take two objects from the stack, perform a comparison and leave a 1 on the stack if the test was true, and a 0 if the test failed. The true condition is always represented with the number 1, while the false condition is always represented with the number 0.+=== Analytic Functions ===
  
 +Analytic functions are a subset of the functions that conform to the layman's concept of mathematical function: they take one or more numerical arguments and return one. Usually are continuous and/or differentiable and include trigonometric (''SIN'', ''ACOS'') and hyperbolic (''LN'', ''TANH'') functions.
 ---- ----
  
Line 46: Line 73:
 Programs may take values from the stack, just like any RPL operator or command. To use values from the stack, a program simply needs to assume they are there and operate on them. Our previous example could be rewritten as: Programs may take values from the stack, just like any RPL operator or command. To use values from the stack, a program simply needs to assume they are there and operate on them. Our previous example could be rewritten as:
  
-<code>« + 2 / »</code>+<code>« 
 +  + 
 +  2 / 
 +»</code>
  
 This short program runs the operator + as its first command. Previously, we had included in the program the numbers 1 and 2, which were put on the stack. This version now expects the user to leave two numbers on the stack before running this program. The program will then add them together, and later divide the result by two (computing the average). This short program runs the operator + as its first command. Previously, we had included in the program the numbers 1 and 2, which were put on the stack. This version now expects the user to leave two numbers on the stack before running this program. The program will then add them together, and later divide the result by two (computing the average).
Line 61: Line 91:
 Variables are created with the comand ''STO'' (store) which stores an object into a named variable. Variables are created with the comand ''STO'' (store) which stores an object into a named variable.
  
-<code>« 4 'X' STO »</code>+<code>« 
 +  4 'X' STO 
 +»</code>
 The program above stores the number 4 in a variable named ''X''. Notice the name of a variable must be enclosed in single quotes when it refers to the name itself, and without quotes when it refers to the content of a variable. In this case, we want the name ''%%'X'%%'' The program above stores the number 4 in a variable named ''X''. Notice the name of a variable must be enclosed in single quotes when it refers to the name itself, and without quotes when it refers to the content of a variable. In this case, we want the name ''%%'X'%%''
  to be put on the stack, to be used by the ''STO'' command.  to be put on the stack, to be used by the ''STO'' command.
Line 91: Line 123:
 To make it even more useful, variables appear in the soft menus, so instead of typing the word ''AVERAGE'' every time, simply using the corresponding key will also run the program and compute the average. To make it even more useful, variables appear in the soft menus, so instead of typing the word ''AVERAGE'' every time, simply using the corresponding key will also run the program and compute the average.
  
-''3'' ''5'' ''APPS(G)'' +''3'' ''5'' (press the key **G**
-will do the same, assuming there were no other variables, so that ''AVERAGE'' was shown on the first spot of the second soft menu, activated with the ''G'' key.+will do the same, assuming there were no other variables, so that ''AVERAGE'' was shown on the first spot of the second soft menu, activated with the **G** key.
  
 ---- ----
Line 321: Line 353:
  
 <file> <file>
-<< 10 1 START "HELLO" -1 STEP >>+« 10 1 START "HELLO" -1 STEP »
 </file> </file>
 This is equivalent to the previous example, except it counts from 10 to 1, adding -1 at each increment. This is equivalent to the previous example, except it counts from 10 to 1, adding -1 at each increment.
Line 340: Line 372:
  
 <file> <file>
-<< 1 10 FOR J "HELLO" NEXT >>+« 1 10 FOR J "HELLO" NEXT »
 </file> </file>
 This example is the same as the ''START'' example, except that after the ''FOR'' statement, we are required to provide the name of a local variable that will become the index counter for the loop. In this case we used the name ''J'' (arbitrarily). The example doesn't use ''J'', so it could've been written using a ''START'' loop as we previously did. However, the following: This example is the same as the ''START'' example, except that after the ''FOR'' statement, we are required to provide the name of a local variable that will become the index counter for the loop. In this case we used the name ''J'' (arbitrarily). The example doesn't use ''J'', so it could've been written using a ''START'' loop as we previously did. However, the following:
  
 <file> <file>
-<< 1 10 FOR J J NEXT >>+« 1 10 FOR J J NEXT »
 </file> </file>
  
Line 353: Line 385:
  
 <file> <file>
-<< 1 10 FOR J J 2 STEP >>+« 1 10 FOR J J 2 STEP »
 </file> </file>
 will leave the numbers 1,3,5,7,9 on the stack. will leave the numbers 1,3,5,7,9 on the stack.
  
 +
 +The behavior of the ''NEXT'' statement is different from the ''STEP'' statement:
 +  * ''NEXT'' always increments the counter by 1, while ''STEP'' could go either direction or leave the same value.
 +  * ''NEXT'' always checks if the counter is ≤ end value to continue the loop. ''STEP'' checks if the counter is ≤ or ≥ end value depending on the direction of the loop.
 +
 +When using ''STEP'', the direction of a loop is determined by the initial start and end values. If the end value is higher than the start value (therefore counting up), the loop will continue while the variable is less than or equal to the end value. On the contrary, if the end value is lower than the start value, the loop will continue as long as the variable is greater than or equal to the end value.
 +
 +When ''STEP'' is used, the magnitude and direction of the step do not affect the condition to end the loop. For example:
 +
 +<file>
 +« 1 'S' STO 
 +    1 10 FOR J 
 +        J 
 +        IF J 4 == THEN -1 'S' STO END
 +        IF J 1 == THEN 2 'S' STO END
 +    S STEP
 +»
 +</file>
 +
 +In the above example ''J'' will count from 1 to 10 (therefore this is a loop counting up) with a step ''S''. The loop begins with a step of 1, being stored in ''S''. The loop will run leaving the values of J in the stack, initially 1,2,3,4 then the condition in the ''IF'' statement becomes true and the step changes to -1.
 +The loop will now begin counting down 3,2,1 and the second ''IF'' statement becomes true, changing the step to 2. The loop will continue running 3,5,7,9, and then the loop will exit when the counter is >10. The complete sequence generated by the code above is then 1,2,3,4,3,2,1,3,5,7,9.
 +
 +When ''NEXT'' is used, the direction is always assumed to be up regardless of start and end values, therefore the following loop:
 +
 +<file>
 +« A 10 FOR J J NEXT »
 +</file>
 +
 +Will produce the following outputs, depending on the value of ''A'':
 +
 +  * ''A=1'' will produce 1,2,3,4,5,6,7,8,9,10
 +  * ''A=10'' it will produce 10
 +  * ''A=11'' it will produce 11
 +
 +Notice that despite the starting value being higher than the end value and the loop using ''NEXT'' (therefore it's a loop counting up), the loop executed at least once. This is because the decision to enter the loop is made when ''NEXT'' is reached.
 +
 +Now let's review a loop using ''STEP'' with a positive step:
 +
 +<file>
 +« A 10 FOR J J 1 STEP »
 +</file>
 +
 +Will produce the following outputs, depending on the value of ''A'':
 +
 +  * ''A=1'' will produce 1,2,3,4,5,6,7,8,9,10
 +  * ''A=10'' it will produce 10
 +  * ''A=11'' it will produce 11,12,13,14,15,... infinite loop
 +
 +Notice that while the loop appears to count up by looking at the code, the condition A>10 causes the loop to be counting down because the start value is larger than the end value, and therefore it will only end when J<10, regardless of the magnitude and direction of the step.
 +
 +In situations where it is desirable to keep the direction of the loop fixed, the ''FORUP'' and ''FORDN'' commands can be used in lieu of ''FOR''. These two variants have two advantages:
 +  * The direction of the loop is fixed and known, regardless of the values the limits might have at run time.
 +  * The loop has one additional check when the word ''FORUP'' or ''FORDN'' is executed. This allows for early exit without being forced to execute the loop clause.
 +
 +Back to the example used above for ''NEXT'' but in this case using FORUP to force the loop to count up:
 +
 +<file>
 +« A 10 FORUP J J NEXT »
 +</file>
 +
 +Will produce the following outputs, depending on the value of ''A'':
 +
 +  * ''A=1'' will produce 1,2,3,4,5,6,7,8,9,10
 +  * ''A=10'' it will produce 10
 +  * ''A=11'' it will not produce any output
 +
 +It differs from using ''FOR'' in the last case: the clause between ''FORUP'' and ''NEXT'' is never executed because the starting value was already larger than the end limit.
 +
 +On the other end:
 +<file>
 +« A 10 FORDN J J -1 STEP »
 +</file>
 +
 +  * ''A=1'' will produce no output
 +  * ''A=10'' it will produce 10
 +  * ''A=11'' will produce 11,10
 +
 +Note that ''NEXT'' always considers the loop direction going up, therefore ''NEXT'' is not compatible with ''FORDN'' which contradicts that assumption.
 +
 +Summarizing, the main differences between all the ''FOR'' constructs:
 +
 +     * ''FOR'' may count up or down, and may be used with both ''NEXT'' and ''STEP''. The loop clause is always executed at least once.
 +     * ''FORUP'' always counts up, may be used with ''NEXT'' or ''STEP''. The loop clause is not executed if the starting value is greater than the end value.
 +     * ''FORDN'' always counts down, can only be used with ''STEP''. The loop clause is not executed if the starting value is less than the end value.
 +
 +
 + 
 === DO loops === === DO loops ===
 The syntax for this construct is: The syntax for this construct is:
-DO .... UNTIL ... END+<file> 
 +DO .... UNTIL testClause END 
 +</file>
  
-Unlinke FOR and START, this loop has no counter. It repeats everything between DO and END, as many times as needed until the test clause (which is everything between UNTIL and END) becomes true.+Unlike ''FOR'' and ''START'', this loop has no counter. It repeats everything between ''DO'' and ''END'', as many times as needed until the ''testClause'' (which is everything between UNTIL and END) becomes true.
  
-Normally, the test clause will contain some test that leaves a true/false result on the stack. When the word END is executed, it will take one argument from the stack and will verify if it's true or false. If it's true, it will not repeat the loop, if it's false, it will start repeating everything after the DO keyword.+Normally, the ''testClause'' will contain some test that leaves a true/false result on the stack. When the word ''END'' is executed, it will take one argument from the stack and will verify if it's true or false. If it's true, it will not repeat the loop, if it's false, it will start repeating everything after the ''DO'' keyword.
  
-In newRPL, a condition is said to be false if it's the number zero. Anything else will be considered true, including things similar to the number zero, like a (0,0) complex number, or a zero vector [0 0]. Both of them will be considered true.+In newRPL, **a condition is said to be false if it's the number zero**. Anything else will be considered true, including things similar to the number zero, like a (0,0) complex number, or a zero vector [0 0]. Both of them will be considered true.
  
-<10 'X' LSTO DO X X 2 / 'X' STO UNTIL X 2 <= END >+<file> 
-The example above creates a local variable X and then starts a DO loop. In the loop the value of X is left on the stack (the first X) , then halved (by X 2 /) and stored back. This is repeated until the condition X<=2 is met. At the end of the program, the numbers 10, 5, and 2.5 will be left on the stack. FOR and START are more adequate when there is a counter that is added or subtracted a quantity every cycle. In this example, while X could be seen as a counter of some sort, it is halved on each cycle, so the FOR and START loops can't be used.+« 10 'X' LSTO DO X X 2 / 'X' STO UNTIL X 2 ≤ END » 
 +</file
 +The example above creates a local variable ''X'' and then starts a ''DO'' loop. In the loop the value of ''X'' is left on the stack (the first ''X'') , then halved (by ''X 2 /'') and stored back. This is repeated until the condition ''X 2 ≤''  is met. At the end of the program, the numbers 10, 5, and 2.5 will be left on the stack.  
 + 
 +''FOR'' and ''START'' are more adequate when there is a counter that is added or subtracted a quantity every cycle. In this example, while ''X'' could be seen as a counter of some sort, it is halved on each cycle, so the ''FOR'' and ''START'' loops can't be used.
  
 === WHILE loops === === WHILE loops ===
 The syntax for this construct is: The syntax for this construct is:
 +<file>
 WHILE ... REPEAT ... END WHILE ... REPEAT ... END
 +</file>
  
-The syntax for these loops is WHILE (something is true) REPEAT (this) END. Anything between the WHILE and REPEAT is a test clause, and has to leave a true/false result on the stack. If the result is true, anything between REPEAT and END will be executed, otherwise the loop ends and the program continues at the END statement.+The syntax for these loops is ''WHILE'' (something is true) ''REPEAT'' (this) ''END''. Anything between the ''WHILE'' and ''REPEAT'' is a test clause, and has to leave a true/false result on the stack. If the result is true, anything between ''REPEAT'' and ''END'' will be executed, otherwise the loop ends and the program continues at the ''END'' statement.
  
 Same as before, a condition is said to be false if it's the number zero. Anything else will be considered true. Same as before, a condition is said to be false if it's the number zero. Anything else will be considered true.
  
-These loops are similar to the DO loops with some important differences:+These loops are similar to the ''DO'' loops with some important differences.
  
-WHILE loops have the test clause at the beginning of the loop, and if the result is false, the loop will not execute at all. DO loops, on the contrary, do one full loop execution before reaching the UNTIL test clause, so even if it results false, the loop was executed at least once. +''WHILE'' loops have the test clause at the beginning of the loop, and if the result is false, the loop will not execute at all. ''DO'' loops, on the contrary, do one full loop execution before reaching the ''UNTIL'' test clause, so even if it results false, the loop was executed at least once. 
-WHILE loops cycle when the clause is true. DO loops cycle when the clause is false. +''WHILE'' loops cycle when the clause is true. ''DO'' loops cycle when the clause is false. 
-Let's write the previous example using a WHILE loop:+Let's write the previous example using a ''WHILE'' loop: 
 + 
 +<file> 
 +« 10 'X' LSTO WHILE X 2 > REPEAT X X 2 / 'X' STO END » 
 +</file>
  
-<< 10 'X' LSTO WHILE X 2 > REPEAT X X 2 / 'X' STO END >> +Again, the result will be the numbers 10, 5 and 2.5 left on the stack. Notice that now the loop will execute when ''X 2 >''. Before, the loop would run until ''X 2 ≤'' which is a different way of stating the exact same thing.
-Again, the result will be the numbers 10, 5 and 2.5 left on the stack. Notice that now the loop will execute when X>2. Before, the loop would run until X<=2 which is a different way of stating the exact same thing.+
  
 ==== Additional sources ==== ==== Additional sources ====
 Since the newRPL is based on the userRPL designed for HP calculator, the basic syntax is shared between those two languages so many examples can be found checking the resources shared by the HP calculator community (in particular for calculators of the series HP 48, 49, 50 ). Since the newRPL is based on the userRPL designed for HP calculator, the basic syntax is shared between those two languages so many examples can be found checking the resources shared by the HP calculator community (in particular for calculators of the series HP 48, 49, 50 ).
  
-Please see the [[manual:appendix:links|Userful resources]] page.+Please see the [[manual:appendix:links|Useful resources]] page.
  • manual/chapter5/basics.txt
  • Last modified: 2021/09/15 10:32
  • by claudio