DEV Community

Cover image for Let the FORTH be with you
Eckehard
Eckehard

Posted on • Updated on

Let the FORTH be with you

Not many readers will know the progamming language FORTH, that was developed in 1970 by Charles H. Moore (not THIS Moore), who was programming on an IBM 1130 computer in Fortran, ALGOL, PL/I and Assembler. The 1130 was called a "third-generation computer", which had about the cpacity of an arduino nano, but the peripherials like a card puncher/reader had the size of a fridge.

Moore wanted a more flexible and interactive approach, so he created a new language, which he thought could be a perfect match for the upcomin micro-comupters, which where called the "fourth-generation". As the 1130 permitted only five-character identifiers, FOURTH became FORTH, a nicer play on words anyway.

FORTH featured an ingenious simple concept, using a stack as it´s only memory. Every input was pushed ontop of this stack, regardless of its type: Constants, operators and even function definitions could be pushed. So, mathematical operations used a inverted polish notation. To add two number, just write

3 4 + .[RETURN]
7
Enter fullscreen mode Exit fullscreen mode

Commands are executed immediately after pressing return, the dot at the end advises the program to print the result.

Function definitions started with a ":" followed by a function name and end with a ";". So, a function was defined like this:

: addFour 4 + ;
3 addFour . [RETURN]
7 
Enter fullscreen mode Exit fullscreen mode

All functions are stored in a dictionary, that is growing with every definition. So, writing a program just meant to start with an empty directory and add more and more functions. Finally, the last function was called to execute the program. (If you want to learn more, there is still an active community and a nice online documentation.

The core of FORTH system can be increadibily small (only some kB), and most of the core was already programmed in FORTH itself. As FORTH can act as an operation system too, it gained some popularity in a varity of application field.

Though FORTH is available for almost any computer system as open source, it never reached a larger community for some reasons:

  • Using stack oriented operations is very machine-friendly, but not easy to understand.
  • The lack of more advanced structures made it very hard to follow the program logic. Reading and understanding a program was nearly impossible, even if you created the code yourself 14 days ago.

Nevertheless, FORTH programmers regularly won speed programming contests as programming in FORTH may be very efficient and compact.

So, what can we learn from this?

Maintainability and human readability are very important for any programming paradigm. Maybe you find a very tricky and smart way to solve a problem. If you are not able to understand your solution some days later, maybe the solution was not a s good as you thought?

Today, we find lot´s of discussion about OO vs Functional Programming. Even if the one or other approach has some advantages, it is still important care for the human readability.

Top comments (1)

Collapse
 
artydev profile image
artydev

☺ i still have a book I bought many years ago Turbo Forth from Marc Petreman...