|
Home Introduction Using Holon Holon Tools Contact |
Forth versus C: The function main In their book, The C Programming Language, Kernighan and Ritchie introduce a function power and a function main, which is used to test the power function. Let us start our comparison with the function main:
The Forth version is: : main ( -- ) \ test power function 10 0 do i . 2 i power . -3 i power . cr loop ; Obviously Forth approaches programming from a different point of view, which we shall now explore. Data StackForth is a stack based language. The Forth functions take the arguments from the stack and leave the result on the stack ready for the following function. Example: ... 2 3 power ... The numbers 2 and 3 are pushed on the stack. Power fetches the values and replaces them by the number 8. do ... loop10 and 0 define the range of the do...loop. The loop index i steps through the values 0, 1, .. 9. The word loop increments the index by one and tests if the upper limit has been reached. The loop index is available inside the loop by the word i . WordsIn Forth everything is a word or a number. Functions, procedures, assembler routines, variables, objects and so on, are all words. The words are collected in a dictionary. New words are defined by existing words, like in a natural language. Building blocksForth words are small functions that do just one thing, and do it right. The Forth system provides many tools, which the programmer uses to build custom words for the application at hand. Example: Instead of a general print function printf(), Forth uses explicit print words. The Forth system offers a couple of standard words like . and .R . These words suffice in many cases, as in our example function main. The word . takes a number from the stack and prints it with a trailing space. It prints the number in the current number base, which is decimal by default. Since the handling of printing illustrates the different points of view of C and Forth, let me elaborate. Imagine that I want to print numbers in three digits, with leading zeros for numbers smaller than 100. The word . prints only the valid digits. Thus I need a custom print word, say .ddd . Here is the definition:
Forth provides the building blocks <#, #, and #>, which format the numeric output ready for the word type. For more details have a look at the definition of the words in the HolonForth systems. You create exactly the printing words that you need for your application. This is simple and clear. The word CR creates a new line (it outputs the Carriage Return code (and also Line Feed, depending on the host OS)). InterpreterThe user interface of Forth is a command line interpreter. You can call and execute every word in the interpreter. If you enter a number, the interpreter puts it on the stack. Thus you pass data to a word in the most natural manner. Write "345 .ddd" and press the Enter key. You get instant results. C needs main in order to test the power function. In Forth the word power can be executed directly. Enter "2 3 power . " and see the result. Thus you can instantly test a word with different sets of parameters. You can, of course, also define test words like main, which exercise a function over a large range of data. You don't have to recompile the program. The test word is added to the existing words and you can call it immediately in the interpreter.
|
|