Home > Previous Page >  Gil Filbey - Forth Series Demo Three
Archive Search  

Languages

FORTH

Gil Filbey

Readers new and old are reminded that the author's introduction to FORTH was published in previous issues of our magazine.

This month we develop a programme which makes use of random processes to serve as a model of diffusion, or just as a game of chance if you prefer it. It also illustrates particular techniques in FORTH.

   Programming in FORTH is essentially well structured. There is, for example, no 'GOTO' statement. Many programmers, brought up on BASIC, miss it. Of course all languages support some form of disciplined 'GOTO'. A conditional loop is such a form. The point is that you lay down pointers which announce' at the outset Where you will jump to if a certain condition is or is not met. This improves the readability of the programme.

  The 'BEGIN ... END' statement is such a structure. If there is'a zero on the stack on arrival at 'END', then the programme goes back to 'BEGIN' (which is the pre-arranged marker). Otherwise it skips over 'END' to the next word.

  So 'GOTO' is reinstated in a disciplined fashion. You can use a 'I' to escape from the loop, or a ZERO to stay in, The following programme illustrates the technique for nested 'BEGIN END' loops. The programme simulates a particle, say a gas molecule, which travels along until it reaches a barrier which it has a definite probability of traversing. If it doesn't pass through it bounces back. There is a succession of such barriers to get through.

  The question is, 'What is the probability that it gets through the lot?' A counter is incremented each time it succeeds in doing so and this result is divided by the number of attempts made. If the process is displayed on a graphics screen you can gamble on its chances.

  This programme contains four barriers, and five nested loops. Words such as ON and 2ON are defined to mean what the particle does when it passes the first bar rier. 'RETNE' ('ENTER' backwards) is a bounce off the first barrier; '1BACK' is a bounce off the second; the 'ATEST' and 'BTEST' are the transmission probabilities on the way forward and on the way back; 'EXIT' is the passing of the last barrier, So after 'EXIT' you need tickets to get out of four loops. These are provided by the '1 1 1 1' which appear after 'EXIT' in the programme on screen 3, The zero which traps you in the final loop is put on the stack at the beginning. The outermost 'IF' mates up with the outermost 'ELSE', ie. 'IF' 1ON ... ELSE RETNE' represents the decision on arrival at the first barrier, having just conducted an 'ATEST' to see whether it is to go on or go back. The programme is developed in three 'screens' (this is the name given to programme memory blocks in the version Lam using) Screens 1 and 2 define words to put a moving point in five sections of the VDU Screen 3 contains the actual programme.


DEMONSTRATION
THREE


A Model of Diffusion




  I am not pretending that this is good programming. You don't need more than two nested loops for any number of bar- riers with a variable to state which barrier you are at. Try it


( SCREEN 1 BARRIERS )
( ***************** )

: WAIT 0 DO LOOP ;
: ENTER 36 0 DO WHITESPOT I 80 10 WAIT
 PLOT ERASE I 1 - 80 10 HAIT PLOT LOOP ;
: RETNE 34 0 DO WHITESPOT 33 I - 80 10
WAIT PLOT ERASE 34 I - 80 10 WAIT PLOT
LOOP ;
: 1ON 60 1 DO WHITESPOT I 36 + 80 PLOT
10 WAIT ERASE I 35 + 80 10 WAIT PLOT
LOOP ;
: 2ON 60 1 DO WHITESPOT I 96 + 80 10
WAIT PLOT ERASE I 95 + 80 10 WAIT PLOT
LOOP ;
: 3ON 60 1 DO WHITESPOT I 156 + 80 10
WAIT PLOT ERASE I 155 + 80 10 WAIT PLOT
LOOP ;
; S

( SCREEN 2 BARRIERS )
( ***************** )

: EXIT 41 1 DO WHITESPOT I 216 + 30 10
WAIT PLOT ERASE I 215 + 80 10 WAIT PLOT
LOOP ;
: 1BACK 60 1 DO WHTTESPOT 94 I - 80 10
WAIT PLOT ERASE 95 I - 80 10 WAIT PLOT
LOOP ;
: 2BACK 60 1 DO WHTTESPOT 154 I - 80 10
WAIT PLOT ERASE 155 I - 80 10 WAIT PLOT
LOOP ;
: 3BACK 60 1 DO WHTTESPOT 214 I - 80 10
WAIT PLOT ERASE 215 I - 80 10 WAIT PLOT
LOOP ;
;S

( SCREEN 3 BARRIERS )
0 VARIABLE TRIALS 0 VARIABLE WINS
0 VARIABLE P
: PROBABILITY 3 255 P  @ - 256   AB/ ;
: ATEST RANDOM RPR P @  > ;
: BTEST RANDOM RPR P @  < ;

: BARRIERS 0 BEGIN  ENTER ATEST
BEGIN TF 1ON ATEST BEGIN IF 2ON ATEST
BEGIN IF 3ON ATEST BEGIN IF EXIT
                   1 WINS +! 1 1 1 1
ELSE 3BACK BTEST 0 1 THEN END
ELSE 2BACK BTEST 0 1 THEN END
ELSE 1BACK BTEST 0 1 THEN END
ELSE RETNE         1 THEN END
1+ OUP TRIALS @ >         END DROP
TEXT HOME
6 DOWN CR." WINS=" WINS @ . CR CR
 ." TRIALS=" TRIALS @ . CR CR
 ." is PROBABILITY PER BARRIER="
PROBABILITY CR CR
 ." TOTAL PROBABILITY=" 3 WINS @ TRIALS @
 AB/  ; ;S
 

  Here, to finish, is a programming problem which I call 'Instant Ben Nicholson' (if you don't get the context refer to Brian Reffin Smith). The objective is to place on the Apple screen random-sized squares in random positions. The screen has a fixed window size and we need to avoid over- lapping the edges. FORTH programming uses top-down design so we start by assuming we have a word which will do the trick. Call it 'TEST' or any other word you care to choose. We will also need a word called 'SQUARE', and a pair of random numbers to fix the centre.

  We then want a random variable to decide the length of the side. By means of this we can locate the four corners and join them up with a line. 'LINE' has already been defined, We must first, however, check that it will mot overlap the edge of the screen and move it in till it doesn't.


  We already have a word 'RANDOM' so we can define a word 'RGET' to generate a random number and put it on the stack. Finally, the random number we have been using lies between 0 and 255 (one byte)


  The square could be too big for the screen You can get over this by forming a logical 'and' with 63; this is a 'bit' comparison and reduces the range to 0 to 63.


  Lastly I Suggest you make the 'TEST' word an open-ended loop so that, for ex: ample, '10 TEST' will put 10 squares on the screen.


  The whole thing looks more interesting, if-a delay is included in the loop. Then, having decided just what you want, you write the programme from the bottom up so that each word in the programme makes use only of words already defined

Gil Filbey lectures in Physics of the Polytechnic of the South Bank, London