Home > Previous Page >  Forth Demo One Gil Filbey May Computer Age.
Archive Search  

Languages

FORTH

Gil Filbey

DEMONSTRATION
ONE

Newcomers to our magazine can refer to the introduction to FORTH which appeared in Issues one to four.



This is the first of 'Statdist' demos. It is a short programme for displaying a negative exponential distribution. A random number programme shows how code may be written into the dictionary by use of 'C, to enter a single byte and to enter a two-byte instruction. The Forth word definition starts with a colon as usual and is then followed by ';CODE', after which follows the set of code instructions. The word so defined must be invoked once before anything else is entered. After that invoking the defined word executes the code. This random number generator adds 13 bytes to give the 14th. Thereafter cycling on the Yindex. In the Statdist commands the word 'INIT' sets up the speeds, Random' generates the number. And RPR' puts it on the stack. The 'NEGEX' programme contains explanatory notes in brackets which the compiler ignores. 20000 is chosen for the end condition since



( SCREEN 2. PROGRAMME FOR SHOWING STAT-
ISTICAL DISTRIBUTIONS. )
     *****************

B VARIABLE T ( IN ALL DEMOS <255-T>/256
IS THE SUCCESS PROBABILITY )

@ VARIABLE Z ( ASSIGNS THE NUMBER OF
GOES IN THE POISSON DEMO.)

@ VARIABLE X 580 ALLOT ( ALLOTS 250
LOCATIONS FOR AN ARRAY X )
0 VARIABLE A ( USED TO INDEX X )

: ZERO ( FILLS X WITH ZEROS ) 250 0 DO
0 X I DUP + + ! LOOP;
: START ( SETS UP POISSON DEMO ) INIT
ZERO A ! HGR2 ; ( USE'N START' N IS
STORED IN A )

: INC 1 SWAP +! ; ( 'A INC' INCREMENTS
A )

: XA ( SELECTS ELEMENT X<A> ) X A C@
DUP + + ;
: WINS ( EXPECTED NO. OF SUCCESSES IN N
GOES ) 256 Z @ */ 255 SWAP - T ! ;
( REVERSE POLISH MIND! ) ;S

it gives a reasonable format on the Apple screen. The success rate is about one in 5 if T is made 200 so some 100000 random numbers are used for the demo. When dividing by 'A' as a scale factor the result is rounded even though this is a bit frivolous, just to show how it is done. The remainder is doubled and compared with 'A'. The 'Begin . . . . End' construction needs explanation. It sets up an indefinite loop which carries on while 'End' sees zero on top of the stack. It will stop when the '20000 'comparison is true.

  Now, as a small exercise in FORTH, write a programme using 'BEGIN.. END' to find the highest common factor of two numbers. The best algorithm is due to Euclid, in which the smaller is subtracted from the larger. The result and the smaller of the two are used as the next pair.

  Gil Filbey is a lecturer in Physics at the Polytechnic of the South Bank, London.


( SCREEN 4. RANDOM NUMBER GENERATOR.
ADDS 0D BYTES AND STORES SUM IN B,Y.
Y RUNS ROUND THE SET OF BYTES. THE
RESULT IS STORED IN $7388.)

: RANDOM ;CODE 1A5, 265, 365, 465 , 5
65, 665, 765, 855, 965, A63, B65 ,


C65, D85 , AC C, 7300 , 93 C, 0 ,
 6785
88 C, 9F0 , 8C C, 7300 , B9 C, 0 , 40 C,
 201F , 0DA0 , 8C C, 7300, 4C C, 201F ,

( SCREEN 5. STATDIST PROGRAMME )
( ******************* *******

: NEGEX
   BEGIN 250 0
    DO
     RANDOM RPR       ( GETS RANDOM NO.)
     T @ >                ( SUCCESSFUL?)
     IF                    ( SUCCESSFUL)
      X I DUP + + INC ( INC. ITH. ELMNT)
       I X I DUP + + @         (GET IT )
       A C@ /MOD   ( DECIDE SAMPLE SIZE)
       SWAP DUP + A C@ >      (ROUND IT)
       IF                       ( TRUE?)
        +1                       ( TRUE)
        THEN       ( JUMP HERE IF FALSE)
       PLOT LEAVE       (PLOT & GET OUT)
      THEN 5  ( JUMP HERE IF NO SUCCESS)
     +LOOP              ( AND TRY AGAIN)
     1+ DUP 20000 >   ( BACK TOʻBEGIN')
   END DROP ;   (OR STOP & DROP COUNTER)
;S