CHAPTER 7. 4TH FUNDAMENTALS
7.23
REPEAT-UNTIL constructs
76
The counterpart of WHILE-DO constructs is the REPEAT-UNTIL construct. This executes the body, then checks a condition at ’UNTIL’. If the expression evaluates to FALSE, it branches back to the top of the body (marked by ’BEGIN’) again. It executes at least once.
This program calculates the largest common divisor.
: lcd begin swap over mod dup 0= until drop . ;
\ body
\ condition
If you enter "27 21 lcd" the programs will answer "3".
7.24
Infinite loops
In order to make an infinite loop one could write: begin ." Diamonds are forever" cr 0 until
But there is a nicer way to do just that: begin ." Diamonds are forever" cr again
This will execute until the end of times, unless you exit the program another way.
7.25
Including source files
4tH has a vocabulary of over 200 words. If you use them in one of your 4tH programs 4tH will recognize them instantly. These words are internal.
But if you take a look at the glossary, you’ll find that there are a lot of other words too.
Words that 4tH will not recognize; they have to be included first. These words are external.
These words are defined in an include file. An include file is just an ordinary ASCII file with 4tH source. You can read them if you want. In order to use these words, you have to tell 4tH where it can find the include file.
This is done by the ’[NEEDS’ directive, which is equivalent to the COMUS word ’IN-
CLUDE’ (which 4tH also supports). Everything up to the next ”]” is considered to be a filename, so the path may contain embedded spaces. You can use absolute paths or relative paths, just make sure that you’re starting 4tH from the proper directory. E.g. this one includes additional ANS-Forth CORE-words from the directory just above ’lib’
1
:
[needs lib/anscore.4th]
Or: include lib/anscore.4th
4tH comes with a rich library of words, which covers a large part of ANS-Forth and CO-
MUS
2 standard words and beyond. They are all located in the ’lib’ directory. In the next level we’re going to need a lot of these words, so you’d better know how to include them.
1
If you’re not sure where that is, enter the ’lib’ directory and execute “cd ..”.
2
In case you wonder, COMUS stands for COMmon USage.