Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
manual:chapter5:basics [2017/10/03 06:04] pier4r |
manual:chapter5:basics [2021/09/15 10:32] (current) 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: | ||
- | < | + | < |
+ | « | ||
+ | | ||
+ | »</ | ||
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: | ||
- | < | + | < |
+ | | ||
+ | | ||
+ | »</ | ||
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 |
+ | |||
+ | 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 ('' | ||
+ | |||
+ | === Operators === | ||
+ | |||
+ | Operators manipulate objects, returning results. Usually these objects are [[manual: | ||
+ | |||
+ | 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 '' | ||
+ | |||
+ | The basic arithmetic operators work as expected ('' | ||
+ | |||
+ | === Functions === | ||
+ | |||
+ | Functions, like operators, manipulate objects and return results. They can be inserted into symbolic expressions, | ||
- | 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), | + | === Analytic Functions === |
+ | Analytic functions are a subset of the functions that conform to the layman' | ||
---- | ---- | ||
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: | ||
- | < | + | < |
+ | | ||
+ | | ||
+ | »</ | ||
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 '' | Variables are created with the comand '' | ||
- | < | + | < |
+ | | ||
+ | »</ | ||
The program above stores the number 4 in a variable named '' | The program above stores the number 4 in a variable named '' | ||
to be put on the stack, to be used by the '' | to be put on the stack, to be used by the '' | ||
Line 91: | Line 123: | ||
To make it even more useful, variables appear in the soft menus, so instead of typing the word '' | To make it even more useful, variables appear in the soft menus, so instead of typing the word '' | ||
- | '' | + | '' |
- | will do the same, assuming there were no other variables, so that '' | + | will do the same, assuming there were no other variables, so that '' |
---- | ---- | ||
Line 255: | Line 287: | ||
=== Conditionals: | === Conditionals: | ||
- | |||
The syntax for this construct is: | The syntax for this construct is: | ||
- | CASE ... END | + | < |
+ | CASE | ||
+ | statements | ||
+ | @default ' | ||
+ | END | ||
+ | </ | ||
+ | or | ||
+ | < | ||
+ | CASE | ||
+ | test_clause1 THEN statements END | ||
+ | @do the statements only if the test_clause1 is true and then | ||
+ | @quit the case block | ||
+ | test_clause2 THEN statements END | ||
+ | @do the statements only if the test_clause2 is true and then | ||
+ | @quit the case block | ||
+ | ... @as many tests as needed with their corresponding THEN and END | ||
+ | statements @do this since none of the above test clauses were true | ||
+ | @this is the default clause | ||
+ | END | ||
+ | </ | ||
- | CASE (this test is true) THEN (do this) END (this other test is true) THEN (do that) END ... [as many tests as needed with their corresponding THEN and END] ... (do this since none of the above were true) END | + | Similar to the '' |
- | + | ||
- | Similar to the IF statement, there is a series of test clauses with THEN ... END. The first test clause is evaluated, and if it's true, anything between the following THEN and END statements will be executed, otherwise it will be skipped and execution will continue at the next test clause. In this way, test clauses will be executed until one of them is true. When this happens, the action specified for that test will be executed (immediately after the THEN), and when the END word is reached, execution will skip all other clauses until the end of the CASE statement. If none of the test clauses is true, execution falls trough to the default clause, finished with an END statement. | + | |
For example: | For example: | ||
- | << CASE | + | <file> |
+ | « CASE | ||
+ | X 1 == THEN " | ||
+ | X 2 == THEN " | ||
+ | " | ||
+ | END | ||
+ | » | ||
+ | </ | ||
- | X 1 == THEN " | + | If the variable '' |
- | X 2 == THEN " | + | If the variable '' |
- | " | + | If the variable '' |
- | END >> | + | === START loops === |
- | If the variable X contains the value 1, the first test clause will be true, the string " | + | The syntax for this construct is: |
+ | <code> | ||
+ | startIndex endIndex START codeStatements NEXT | ||
+ | </code> | ||
+ | or | ||
+ | < | ||
+ | startIndex endIndex START codeStatements incrementValue STEP | ||
+ | </ | ||
- | If the variable X contains the value 2, the first clause will be false, execution will continue to the second test clause, which is true in this case, leaving the string " | + | Where '' |
+ | '' | ||
- | If the variable X contains 0, the first and second | + | '' |
+ | |||
+ | This loops takes 2 arguments from the stack: a start and and end count. It repeats the '' | ||
+ | |||
+ | < | ||
+ | « 1 10 START " | ||
+ | </ | ||
+ | Will count from 1 to 10, and therefore repeat 10 times whatever is inside the loop delimiters ('' | ||
+ | |||
+ | When using the '' | ||
+ | |||
+ | < | ||
+ | « 10 1 START "HELLO" | ||
+ | </ | ||
+ | This is equivalent to the previous example, except it counts from 10 to 1, adding -1 at each increment. | ||
+ | |||
+ | The counter | ||
=== FOR loops === | === FOR loops === | ||
Line 290: | Line 369: | ||
</ | </ | ||
- | A FOR loop is almost identical to a START loop, except the counter is made available to the program inside the loop as a local variable. For example: | + | A '' |
- | << 1 10 FOR J " | + | <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' | + | « 1 10 FOR J " |
+ | </file> | ||
+ | This example is the same as the '' | ||
- | << 1 10 FOR J J NEXT >> | + | <file> |
- | will count from 1 to 10, on a variable named ' | + | « 1 10 FOR J J NEXT » |
+ | </file> | ||
- | Similar | + | will count from '' |
- | << 1 10 FOR J J 2 STEP >> | + | Similar to the '' |
+ | |||
+ | <file> | ||
+ | « 1 10 FOR J J 2 STEP » | ||
+ | </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. | ||
- | === START loops === | ||
- | The syntax for this construct is: | ||
- | < | ||
- | startIndex endIndex START codeStatements NEXT | ||
- | </ | ||
- | or | ||
- | < | ||
- | startIndex endIndex START codeStatements incrementValue STEP | ||
- | </ | ||
- | Where // | + | The behavior of the '' |
- | // | + | * '' |
+ | * '' | ||
- | '' | + | When using '' |
- | This loops takes 2 arguments from the stack: a start and and end count. It repeats anything between START and either NEXT or STEP using a counter that goes from the first argument (start) | + | When '' |
- | << 1 10 START " | + | <file> |
- | Will count from 1 to 10, and therefore repeat | + | « 1 ' |
+ | | ||
+ | J | ||
+ | IF J 4 == THEN -1 ' | ||
+ | IF J 1 == THEN 2 ' | ||
+ | S STEP | ||
+ | » | ||
+ | </file> | ||
- | When using the NEXT keyword | + | In the above example '' |
+ | The loop will now begin counting down 3,2,1 and the second '' | ||
- | << 10 1 START " | + | When '' |
- | This is equivalent | + | |
+ | <file> | ||
+ | « A 10 FOR J J NEXT » | ||
+ | </ | ||
+ | |||
+ | Will produce the following outputs, depending on the value of '' | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Notice that despite the starting value being higher than the end value and the loop using '' | ||
+ | |||
+ | Now let's review a loop using '' | ||
+ | |||
+ | <file> | ||
+ | « A 10 FOR J J 1 STEP » | ||
+ | </file> | ||
+ | |||
+ | Will produce the following outputs, depending on the value of '' | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Notice that while the loop appears | ||
+ | |||
+ | In some situations it is desirable | ||
+ | * 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 '' | ||
+ | |||
+ | Back to the example used above for '' | ||
+ | |||
+ | < | ||
+ | « A 10 FORUP J J NEXT » | ||
+ | </ | ||
+ | |||
+ | Will produce the following outputs, depending on the value of '' | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | It differs from using '' | ||
+ | |||
+ | On the other end: | ||
+ | < | ||
+ | « A 10 FORDN J J -1 STEP » | ||
+ | </ | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | Note that '' | ||
+ | |||
+ | Summarizing, | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
- | The counter in a START loop cannot be accessed from inside the loop. | ||
+ | |||
=== DO loops === | === DO loops === | ||
The syntax for this construct is: | The syntax for this construct is: | ||
- | DO .... UNTIL ... END | + | < |
+ | DO .... UNTIL testClause | ||
+ | </ | ||
- | Unlinke | + | Unlike '' |
- | Normally, the test clause | + | Normally, the '' |
- | 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, |
- | << 10 ' | + | <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 ' |
+ | </file> | ||
+ | The example above creates a local variable | ||
+ | |||
+ | '' | ||
=== WHILE loops === | === WHILE loops === | ||
The syntax for this construct is: | The syntax for this construct is: | ||
+ | < | ||
WHILE ... REPEAT ... END | WHILE ... REPEAT ... END | ||
+ | </ | ||
- | 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 '' |
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 '' |
- | 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. | + | '' |
- | Let's write the previous example using a WHILE loop: | + | Let's write the previous example using a '' |
+ | |||
+ | < | ||
+ | « 10 ' | ||
+ | </ | ||
- | << 10 ' | + | Again, the result will be the numbers 10, 5 and 2.5 left on the stack. Notice that now the loop will execute when '' |
- | 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: | + | Please see the [[manual: |