Learning Forth fast and efficient

The good news is, that the Forth language itself is well suited for programming software. Forth can be compared with C but only with less luxury. Like in C, there are not built-in types for object-oriented programming, if the programmer needs that feature he has to build his own object-oriented extension. C makes the usage of arrays very easy, Forth doesn’t even have arrays. But it’s possible to emulate that feature with Forth too. That means, the programmer has to calculate the memory address in which the values are stored by subfunction.

Even the Forth internal stack is not very complicated. Sure, the IF construct works in Forth different than in C, but with a bit manual debugging, it is possible to create in Forth all the same if-then-statements like in any programming language. What makes Forth hard for most developers is the lack of standard libraries, and a situation in which Forth is used at a barebone metal PC, because in such a situation, Forth has to provide the filesystem, the networking connection and the display driver too.

The best way in learning Forth is trying the language out. What Forth has in common with other languages is, that only the language itself is provided but not the algorithm to solve certain problems. If somebody doesn’t know, what Bubblesort is, he will get problems in implementing it in Forth and C. That means, a programming language only provides basic building blocks like functions, for-loops, if-then-statements and variables, but combining these features into useful software is something which is done on top of the features.

The obscurity of Forth is in reality smaller than it looks from the outside. The main reason why Forth is perceived as a complicated language is a mixture of:

– well working existing languages like C++ and Python which provides everything what the programmer need

– missing documentation of Forth plus missing example sourcecode

Forth is often introduced as a stackbased language which is working different to other programming languages. Yes, Forth has a stack, but Forth remains a normal procedural oriented programming language. That means, there are functions, variables and for loops, and the programmer is combining them together into useful code. Forth is harder to learn than other languages, that is right. But writing code in Forth after a while is not very hard. The main bottleneck is not the language itself, but the algorithms.

What makes Forth unique in contrast to Golang, C# or Java is that using existing libraries is either technically not possible or is a sign of bad coding style. The typical C# programmer is asking which libraries he can use, because he want’s to increase his productivity. C# has libraries for string manipulation, graphics, sound and networks. Forth has not a single library. Everything has to be invented from scratch. And the joke is, that even if somebody has written a string library, nobody else will use it.

The number one question for Forth beginners is how they can manage complexity without existing libraries. The answer is, that in mainstream programming languages, libraries are overestimated. For example, Graphics libraries are providing a huge feature-set for drawing all kind of shapes to the screen. The Forth way in doing the same task is to identify the graphics memory and store the pixelvalue direct to the screen. This is done with a newly created word. On top of this word, extra words for drawing lines, drawing rectangles and drawing circles are created. That means, a Forth programmer has to invent his own graphics subsystem. The main reason why Forth is fascinating is because the language motivates the programmers in doing so. That means, during the development, the developer has to read a lot about a subject. And if the Forth code is written he is an expert for drawing primitives.

The main reason, why developers are using Forth is not because they want to solve problems with that language, but they want to learn something about programming, hardware and computer in general. Such projects are motivated by knowledge acquisition. That means, if somebody is interested in compilers, he can write a Forth program and after 6 months he will know very much about compiler writing. His Forth program won’t be working very good, but nobody cares because Forth is some kind of sandbox in which new ideas can be tested out. The same is true for hardware designs, new operating systems, databases and so on. Forth encourages the people to trying out new things and build their own software system from scratch without using out-of-the-box tools written by somebody else.

Let us compare Forth with normal Linux development. The typical Linux developer is using an Intel CPU invented by Intel, he will utilize the C++ compiler from the GNU project, the Linux kernel from Red Hat and the graphics library from SFML. On top of this equipment he creates a game. At the end, around 0.1% of all the codelines are written by the developer itself, but 99.9% were created by somebody else. He has created nothing, instead he takes the work by other and followed the manuals by others. In Forth the relation is the other way around. A Forth program is written with 99.9% by the programmer itself and he has taken nearly nothing from existing repositories.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.