Languages
FORTH
Gil Filbey
Newcomers to our magazine should read the series on FORTH which appeared in Issues 1 to 4. The "Demonstration One" program appeared in Issue 5. First, the answer to May's problem. This was to write a programme for finding the H.C.F. of two numbers. The original Euclidean algorithm required you to divide the larger by the smaller, retaining only the remainder and the smaller from this division. This is then iterated until the two numbers are the same. This supplies the answer. It is slow because division is a slow process. An alternative is to subtract the two numbers instead, stopping when the two are equal For example: : HCF BEGIN DUP ROT  ABS 2DUP = END DROP . : To run this, you type 'A B HCF' where 'A' and 'B' are the two numbers. The loop ends when the two remaining numbers are equal; that is, when the answer to the test '=' is true, which puts a '1' on the stack. If they are unequal the test puts a '0' on the stack so that 'END', seeing a '0', takes the programme back to 'BEGIN'. If you have no version of FORTH available but you can get your hands on a HewlettPackard programmable calculator you can try a modification of the HCF programme on it and get some practice with high stack handling at the same time. The HP calculator has a 4deep stack and has available commands similar to 'DUP' 'ROT' and 'SWAP'. For example '6' 'ENTER' gives a '6' in the display (top of the stack) and another in the nexttotop. So 'ENTER' can be used like 'DUP', 'SWAP' is represented by a key marked X≷Y (X is top, Y is second). 
DEMONSTRATION TWO 

A key labelled 'R↓' will rotate all four stack entries, putting the top to the bottom and moving all the others up, So here is HCF on the HP 29C CALCULATOR (the others are similar). LBL 1 (LABELS THE START) WS (KEY IN THE FIRST NUMBER) R/S (KEY IN THE NEXT NUMBER) LBL 2 (EQUIVALENT to 'BEGIN') ENTER (LIKE 'DUP') R↓ (ROTATES TOP TO BOTTOM)  (SUBTRACTS TOP FROM NEX ABS (GIVES ABSOLUTE VALUE) X≷Y (EQUIVALENT TO 'SWAP') R↓ X=Y (TEST FOR END OF LOOP) RIN (EQUIVALENT TO 'END') GTO 2 (LOOP IF NOT TRUE) This is the state of the stack after each operation. Suppose the two numbers are 175 and 50. R/S(0 0 0 175)R/S(0 0 175 50) ENTER(0 175 50 50)R↓(50 0 175 50) ABS(0 50 0 125)X≷Y(0 50 125 0) R↓(0 0 50 125)  and now we have a pair of numbers to serve for the next time round. The stack will finish up like this (0 0 25 25). The test 
will be true and the programme will stop with 25 on display The FORTH programme is similar but not precisely so. For example, here's what happens to the stack in FORTH HCF  (175 50)DUP(175 50 50)ROT(50 50 175)  ABS(50 125) And we are ready to go round again. We have to do a '2DUP' before the test for equality since the test removes the pair of numbers it tests. HP calculators are useful for learning to handle the stack in FORTH. They will also perform very sophisticated calculations but obviously much more slowly than a microcomputer. We now continue the Stat dist Demo First I want to improve on last month's 'NEGEX' demo. This involves ironing out a bug in the md. no. generator. The 13^{th } Bin is low. This is because there is a slight periodicity caused by the fact that 13 numbers are added together. The error is largely removed by neglecting successes that occur on an odd try after the start. That is, throw my 'die' for a success but if it occurs on an odd try Ignore it. You may think it is cheating to manipulate a random no. like this but it is only the inverse of the process of cleaning up a signal by removing the 'noise'. I remove the odd tries by 'anding' the loop counter with 1. The modified programme follows. STATDIST INSTRUCTIONS ********************* >BLOAD FORTH M/C ROUTINES 20 >BLOAD FORTH STATDIST A 4 >BLOAD FORTH NEGEX 5 ( OR OTHER EXAMPLE) >BRUN FORTH F 4 LOAD 5 LOAD TO RUN NEGEX..EXAMPLE, 200 T ! 20 SAMPLE 8000 NEGEX.. FOR MORE, 2000 NEGEX. FOR MORE ACCURACY, 30 SAMPLE. DOS>BLOAD FORTH FERMI 3 >CALL8192 4 LOAD 2 LOAD TO RUN POISSO..EXAMPLE, 200 T ! 0 SAMPLE 20000 FERHI.. FOR MORE, 20000 FERHI ETC. DOS>BLOAD FORTH POISSON 5 >CALLS8192 4 LOAD 5 LOAD TO RUN POISSON. .EXAMPLE.250 GOES 2 WINS 0 SAMPLE 600 POISSON. FOR MORE..600 POISSON. FOR MORE ACCURACY INCREASE GOES PER WIN. TO DEGENERATE. TAKE E.G 8 GOES 4 WINS. ( SCREEN 5. NEGEX DISTRIBUTION) ( HOW LONG DO YOU HAVE TO WAIT FOR SUCC ESS?) *I treat it as a success by returning to the start but I don't record it. 

Prototype Agritec Motsture Computer 
: NEGEX 0 DO 50 0 DO RANDOM RPR ( GETS RANDOM NUMBER) T @ > ( SUCCESSFUL?) IF ( YES BUT) I DUP 1 AND ( REJECT) IF DROP ( ODD TRIES) ELSE 5 * ( SCALE IT) X I DUP + + INC ( SCOREAHIT) X I DUP + + @ ( GET IT) A C@ / ( DECIDE SAMPLE SIZE) PLOT ( PLOT IT) THEN LEAVE ( AND HOP IT) THEN ( IF YOU'RE HERE YOU FAILED) LOOP ( SO TRY AGAIN) LOOP ; ;S ( SCREEN 3. FERMI DISTRIBUTION ) ( **************************** ) : FERMI 0 DO 50 0 DO RANDOM RPR T @ > ( SUCCESS?) IF I DUP 1 AND ( YES BUT RE) IF DROP ( JECT ODD ONES.) ELSE 5 * RANDOM RPR 3 * 4 / PLOT ( PLOT RND?) THEN LEAVE ( YCOORD. AND) ( EXIT INNER LOOP. ) THEN ( JUMP HERE IF FAILURE ) LOOP ( AND 60 ROUND AGAIN. ) LOOP ; I will try to give an analogue of this. Suppose you had a number of egg boxes laid out in numbered array with each egg hole numbered, say, 1 to 6, within each box. You stop at the first box and throw a die having decided on a number to constitute a success. If you are successful you throw again to decide which egghole to put an egg in and then go back and start again. **If you fail at any box you pass on to ** 'Notice I am using the doctored rnd. no. generator.' 
FORTH
the next (only starting again on a success). If you already have an egg in the indicated hole you do nothing and return to the first box again. It is this last trick that gives rise to the 'Fermi' Distribution. After a time you find the early boxes are full, with a tailoff in the highernumbered boxes. Not being allowed to put an egg in an occupied hole is called the 'Pauli Exclusion Principle'. The actual case in which this arises in Physics is in connection with electrons in solids such as metals and semiconductors. In fact, it is this particular distribution that makes possible the construction of the devices which go to make a modern computer. Anyway, the distribution it self is interesting.
As a tailpiece, I read a recent survey of
languages for micros in 'I.E.E.E. Computer'
in which is said that FORTH has poor
readability because of its 'reverse polish
look'. Fact is that all languages have poor
readability unless you have learned to read
them. When computer languages become
as readable as our everyday language then
I suspect they will be just as imprecise. 
As a computer language FORTH enters it
second decade, the Forth Interest Group announces
the expansion of its newsletter FORTH
DIMENSIONS. Now in a bound, journal form
this newsletter offers FORTH related event
from around the world, with technical and his
torical articles.
The author is a lecturer in Physics at the South Bank Polytechnic, London. 