Languages
FORTH
Gil Filbey
LEARNING FORTH
A miscellany

You notice that there are no rules about brackets i.e. working from the inside out although this is effectively what happens in both these examples. RPN takes care of it. What are the rules then? They are as follows: 1). Place number on stack This goes on to ternary operators and higher. (Try writing a flow chart for the above procedure). A simple example of a nonarithmetic ternary operator is the FORTH word ROT which rotates the top three numbers on the stack. To remind you: 1 2 3 ROT 2 3 1 An example of an arithmetic ternary operator is the FORTH word */ A B C */ evaluates A*B/C It is different, in its effect, fromA B * C / in that the product AB can be up to 31 bits whilst the simple AB*is a 16 bit number. You could even regard the operator: Cos + * + * ; as a quinary operator and so define the word: : WHIZ COS + * + * ; Then in the unlikely event of having to evaluate a large number of expressions of the form X you could write A B C D E WHIZ. Where A B C D E is any set of parameters. Getting up to a few antics such as this really gives you the feel of FORTH. For fear you might be misled I must point out that 'cos' is not a Forth word and if you want to use such trigonometric functions you must define the appropriate words. A further illustration. To evaluate a polynomial 6x^{5}+5 x ^{4}+4 x^{3 }+ 3 x ^{2} + 2 x + 1 This is best written ((((6x + 5) x + 4) x + 3) x + 2) x + 1 If we had x, 5 times in succession on the top of the stack xxxxx We could proceed by 6 * 5 + * 4 + * 3 + * 2 + 1 + Each successive'*'uses up an'x' You could define the word POLY thus : POLY 1 DO DUP LOOP To use it you write 65 POLY Where 6 is a chosen value of x and 5 is the highest power of x. The LOOP gives 5 successive values 6 and the rest evaluates the polynomial. For an amusing example which nonetheless points the way to the use of FORTH in precision arithmetic we define the word AB/ thus, : AB/ CR CR SWAP OVER /MOD % ."." ROT 0 DO OVER 10 SWAP * /MOD % LOOP DROP DROP; The words which require explaining are CR (Carriage return). MOD (Divide 2nd on stack by the top leaving remainder) 

Reverse Polish NotationI t has been suggested by some readers may not be familiar with Reverse Polish Notation (RPN) as used by FORTH and incidentally by BURP (Basic Using Reverse Polish), which is a language used by the Wireless World Scientific computer,* and the whole range of Hewlett Packard calculators. To read a line of arithmetical calculation in RPN you run your eye along until you come to the first operator (such as +, *, cos) and then use it in a logical fashion on the numbers which precede it. Some operators are unary operators, that is to say they need only a single operand (number). Cos is such an operator. Some are binary in that they need two operands. Plus (+) is such and times (*) is such another. 2 Cos means, 'evaluate the cosine of 2' whilst 2 Cos 1 + means 'evaluate the cosine of 2 and add 1'. It helps the clarity if a double space separates cos and 1, 2 cos 1 + The power of RPN lies in the fact that no rules concerning brackets are necessary; neither is an 'equals' sign. Whilst it is an advantage to write down a complicated expression in order favourable to RPN it is 
never necessary. Any expression can be tackled in the order in which it is written. You enter the first number (operand) and if there is an operation you can carry out on it, you do so. Otherwise you leave it there and pass on to the next number. It will wait patiently until numbers above it have been processed and each such process feeds it up. Consider an extreme case, X = 6 (3 + 2 (4 + Cos 2)) Evaluated directly in RPN this is, 6 3 2 4 2 Cos + * + * Five dangling operands before the first operator! You write down each number in succession and then list the operators reading backwards along the line. In FORTH nothing happens till you hit RETURN at the end. That is the way I would do it in FORTH if I were just using FORTH as a calculator I couldn't do it like that, however, on my H.P 29C calculator since the stack isn't big enough. The initial 6 would drop out. It would be necessary to change the order to, ((Cos 2 + 4)2 + 3)6 Evaluated in RPN this is 2 Cos 4 + 2 * 3 + 6 * and no operands are left dangling. * Powertran, designed by J H Adams. 
% (Prints without any space). Use, N AB AB/ Result, it prints out the result of dividing A by B to N decimal places where N is unlimited! Some other features of Appleforth.5. The HEX dump feature is interesting because it gives both the usual HEX listing of a program or definition together with the ASCII values by the side. As an example we list out the definition AB/just quoted. It was written into what is called SCREEN 4 which is part of a memory area set aside for definitions and editing, (There are 6 such screens each of size 1K): The ASCII characters enclosed in brackets are comments to remind the user of the way to use the definition. Anything enclosed between brackets is ignored by the compiler. You notice the word HEX at the beginning. This puts the system into HEX from DECIMAL and it will remain in HEX until you decide to change it. In the original version of FORTH the screens are sectors on a disc; but in the version I am using, and in the PROGRAMMA version for PET and APPLE etc, they are memory locations. When you are in one of these you have the ability to edit your text. In the 1.5 version this is done by means of control commands.

FORTH
When you have finally edited the text to your satisfaction you then type
Comments may be inserted anywhere even within a definition by means of the FORTH word " ("This, being a word, requires a space to be left before the first character of the comment e.g: (THIS IS A COMMENT) The PROGRAMMA INTERNATIONAL version uses a different method. When in a screen you use Line numbering (automatic if you want). The last line of the screen is then used as an edit line. Anything written in it may be inserted as a line into the existing text or used to replace a line of the text. Both of these are simple and powerful editing systems. A Forth ProgrammeTo finish off this rather discursive article we ought to have another program. I choose a digital filter software implementation. A digital filter is a device into which an input stream of data is fed at fixed time intervals. The output at any instant is dependent on the state of the device and the value of the input. The state of the device however is dependent upon previous inputs and outputs. In some way we are all such devices! Our reaction to a stimulus depends upon our experience both of the effect upon us of the stimulus and of the results of our previous reactions. However a digital filter should be relied on not to change its state in the interpulse interval. A simple type of filter is one in which the output depends on the input and the immediately previous output in the following way 
Xn
→▭→ Yn
Yn = K_{1} X_{n} + K_{2} Y_{(n1)} This is called a firstorderlag filter. K1 + K2 = 1 we have a lowpass filter. Such a filter can show, for example the trend in the values of X whilst smoothing out shortterm fluctuations. You might be tempted in this case to write Yn = K_{1} (X_{n}Y_{n1}) + Y_{(n1)} but the truncation errors would be greater in integer arithmetic. We define variables A1 A2 B1 B2 in the usual way: 0 VARIABLE A1 ETC and a variable to denote a time internal. This latter is not necessary in the present case but is useful for scaling a graph (I shall discuss graphics in a later article): 0 Variable T Then: : INIT 0 T ! B2 ! B1 ! A2 ! A1 !; To start we type N N N N INIT). Which establishes the values K1 & K2. Eg: 2 13 11 13 INIT Then: : LAG K1 SWAP K2 DUP . ; USE Y0 X1 LAG SUBSEQUENTLY X2 LAG X3 LAG Etc. X1 X2 X3 ... could be the readings of your electricity meter or any other set of data which requires smoothing. The smaller the value of K1 the greater the smoothing effect. Noisy signals can be cleaned up by storing the signals on tape or directly in memory and feeding them through the LAG programme at leisure. The precision of the arithmetic is generally adequate here but you might like to improve it along the lines of the AB/ example. 
