Forth from a programmer's perspective
Added 31 Jul 2008
Forth relies heavily on explicit use of the stack data structure and Reverse Polish Notation (or RPN, also used on advanced calculators from Hewlett-Packard). This notation is also called postfix notation because the operator comes after its operands, as opposed to the more common infix notation where the operator comes between its operands. The rationale for postfix notation is that it is closer to the machine language the computer will eventually use, and should therefore be faster to execute. For example, one could get the result of a mathematical expression this way:
25 10 * 50 + .This command line first puts the numbers 25 and 10 on the implied stack; the "*" command multiplies the two numbers on the top of the stack and replaces them with their product; then the number 50 is placed on the stack, and the "+" command adds it to the previous product; finally, the "." command prints the result to the user's terminal. Even the language's structural features are stack-based. For example:
300
: FLOOR5 DUP 5 < IF DROP 5 ELSE 1 - THEN ;This code defines a new word (again 'word' is the term used for a subroutine) called "FLOOR5" using the following commands: "DUP" simply duplicates the number on the stack; "<" compares the two numbers on the stack and replaces them with a true-or-false value; "IF" takes a true-or-false value and chooses to execute commands immediately after it or to skip to the "ELSE"; "DROP" discards the value on the stack; and "THEN" ends the conditional. The net result is a function that performs similarly to this function (written in the C programming language):
int floor5(int v) { if (v < 5) return 5; else return v - 1; }
Forth became very popular in the 1980s because it was well suited to the small microcomputers of that time, being very efficient in its use of memory and easy to implement on a new machine. At least one home computer, the British Jupiter ACE, had Forth in its ROM-resident OS. The language is still used in many small computerized devices (called embedded systems) today for efficiency reasons.
Forth is also infamous as being one of the first, and simplest extensible languages. That is, programmers can easily adapt the features of the language to the problem. Unfortunately, extensibility also helps poor programmers to write incomprehensible code. The language never achieved wide commercial use, perhaps because it acquired a reputation as a "write only" language after several companies had product failures caused when a crucial programmer left.
Responsible companies using the language, such as FORTH Inc, had become aware of the problem and addressed it with internal cultures that stressed code reviews.