Pseudo Operators

Home, Up: Operators and Constants

 

This page (unlike the next one) is mostly intended for advanced users.

Like operators, pseudo operators affect arguments to their left and then delete themselves, but unlike operators, they do not perform calculations.

 

On this page:

Swap Pseudo Operator ~, Pseudo Operator WHISK, Pseudo Operator DONE.

 

The Swap Pseudo Operator ~

Many mathematical operations are noncommutative, that is, the order of their arguments is relevant -- e.g., 2 divided by 10 is not the same as 10 divided by 2.

The pseudo operator ~ swaps the two arguments to its left:

? 2 10 /

= 0.2

? 2 10 ~ /

= 5

 

In many cases you could easily write the arguments in the desired order instead of using ~, but there are cases where ~ can be helpful.

For instance, consider the example given on the previous page (divide a number by the sum of several other numbers) to illustrate the use of the n-argument delimiter:

? 27 | 3 2 4 SUM /

= 3

You could perform the same calculation by writing:

? 3 2 4 SUM 27 ~ /

 

Swapping arguments with ~ can be useful when creating user-defined 2- or 3-argument operators (see page User-Defined Operators in chapter "Insert Files and User-Defined Elements").

 

Pseudo Operator WHISK

WHISK removes ("whisks away") the two arguments to its left and stores them under the names A and B, which can then be used in the calculation line.

This is useful when a calculation involves the same values more than once (see also the pseudo constant DUP on page Pseudo Constants later in this chapter), or to create user-defined 2- or 3-argument operators (see page User-Defined Operators in chapter "Insert Files and User-Defined Elements").

 

A simple example, just to show how WHISK works -- calculating the length of the hypotenuse of a rectangular triangle:

? 3 4 WHISK A SQ B SQ + SQRT

= 5

A takes the value of 3, B the value of 4. A gets squared, then B gets squared, then the results are added, and the square root is drawn.

You'd get the same result with 3 SQ 4 SQ + SQRT, but WHISK lets you write the two arguments 3 and 4 next to each other, without an operator in between. We will use this example when we discuss user-defined operators.

 

You can use WHISK several times in one calculation line, each time the previous values of A and B will be overwritten.

A and B can only be used following WHISK within the same calculation line.

The values of A and B are not preserved after the line has been processed -- this is deliberate.

WHISK deliberately does not auto-include $ at the beginning of the calculation line!

 

In some cases you may want to "whisk away" only a single value -- you can do this by putting a dummy value (for instance, 0) before WHISK, and then only use A as often in your calculation line where you need it.

WHISK offers a more flexible way of using the same value several times than the pseudo constant DUP, but DUP has its uses, too.

Where WHISK doesn't solve your problem you have to look at variables, scripts and loops.

 

Pseudo Operator DONE

DONE preserves the number immediately to its left, and deletes whatever numbers may stand to the left of it.

Example:

? 3 4 5 7 + DONE

= 12

The + operator adds 5 and 7 and replaces them with the result 12, then DONE keeps this value but deletes the remaining numbers to the left. Without DONE the calculation would have produced an error due to the remaining surplus arguments.

While it may seem to make little sense to enter numbers only to ignore and delete them, there are actually cases where this feature may be helpful. Admittedly, though, these are rare and DONE is something that you'll probably never need.

 

Home, Up: Operators and Constants, Prev: n-Argument Operators, Next: Constants