[comp.lang.forth] Forth Education: Introductory

ForthNet@willett.UUCP (ForthNet articles from GEnie) (04/12/90)

Category 2,  Topic 3
Message 41        Tue Apr 10, 1990
W.BADEN1 [Wil]               at 19:26 PDT
 
Should Forth be written in Horizontal Format or Vertical Format?

The answer is No.

The difference between the two formats was illustrated in a recent message by
a nominal definition of MAX.

Horizontal Format : MAX 2DUP < IF SWAP THEN DROP ; Vertical Format : MAX
   2DUP
   <
   IF
      SWAP
   THEN
   DROP
   ;

This definition is so simple that it doesn't matter what format is used -- to
someone who is already fluent in Forth.

But for beginners or for publication these definitions are both bad.

Forth deserves its reputation as a write-only language, and these definitions
show why.

The reason that Forth is so susceptible to Write-Only-ness is obvious.

   You can't understand Forth if you don't know what's on the stack. 

The cure is also obvious.

   Make sure that what is on the stack is always known.

How to do this is not so obvious.  At the least you should SHOW and TELL.

   SHOW logical structure by phrasing and layout.

   TELL what is on the stack for every phrase.

The TELL rule does not mean a stack comment for every phrase.  Forth code
generally has "persistence of stack."

When a stack state recurs it is a sure sign of a phrase and is a natural place
for extra spaces.

Let's have another look at the Horizontal Format.

: MAX ( n n -- n) 2DUP < IF    SWAP    THEN    DROP ;

The stack state at the occurrences of extra space is the same as given in the
initial stack effect comment.  The stack stack at the semicolon is also given
in the initial stack effect comment.

Phrasing is forced by the stack states. It is not arbitrary.

Phrasing gives natural places for line-breaks.  As an intermediate format
let's put line-breaks at the phrases.

: MAX ( n n -- n)
   2DUP < IF
      SWAP
   THEN
   DROP ;

"The stack state at the beginning of every line is given by the preceding
stack comment."

This is theoretically sound but many Forth programmers feel uncomfortable with
words like "IF" at the end of a line.  The following modification is what has
worked for me since 1986.

   The Principal Rule for Writing Readable Forth.

   The previous stack comment gives the stack state
   (1) at the beginning of a line not starting with a control flow 
   word; and
   (2) at extra space in the middle of a line. 

Using this the definition should be written

: MAX ( n n -- n)
   2DUP <
   IF
      SWAP
   THEN
   DROP ;

Or, maintaining logical indentation,

: MAX ( n n -- n)
   2DUP <
      IF   SWAP   THEN
   DROP ;

Using the Principle Rule if one word is written per line, the stack comments
would have to be something like --

: MAX ( n n -- n)
   2DUP ( n n n n)
   < ( n n flag)
   IF ( n n)
      SWAP
   THEN
   DROP ( n)
   ;

Here the stack comments that are necessary by the Principal Rule obscure the
meaning.

The best formats are those that have All Necessary Stack Indicators in the
fewest number of lines, and still display the logical structure of the
definition.

Procedamus in pace, Wil.

-----
This message came from GEnie via willett through a semi-automated process.
Report problems to: 'uunet!willett!dwp' or 'willett!dwp@gateway.sei.cmu.edu'

ForthNet@willett.UUCP (ForthNet articles from GEnie) (07/29/90)

 Date: 07-26-90 (20:33)              Number: 1671 (Echo)
   To: MATTHIAS GIWER                Refer#: 1667
 From: DOJUN YOSHIKAMI                 Read: 07-27-90 (11:53)
 Subj: LEARNING FORTH                Status: PUBLIC MESSAGE

 ML = Machine language ... I assume.  I agree.  Machine language makes
 you worry about a few too many things when your attention is better
 elsewhere.  Forth lets you dip into machine code when you really need
 the speed without the overhead of the address interpreter.  Of interest,
 I recently found that F83 is a wee bit faster than Microsoft Fortran
 despite the fact that there is still the overhead for the address
 interpreter.  Interesting, no?  [This was a little program to calculate
 PI, and it used a terrible algorithm...]

 dy ;-)
-----
This message came from GEnie via willett through a semi-automated process.
Report problems to: uunet!willett!dwp or willett!dwp@hobbes.cert.sei.cmu.edu