Contents |
Chapter 6DEFINING NEW ARITHMETIC WORDSNow you know about +, -, * and so on, you have quite a number of building blocks for defining new words. For instance, here is a word to double a number and print the answer:
So where's this number that DOUBLE doubles? Answer - it must already be on the stack when you use DOUBLE. If you want to double 23, you type 23 DOUBLE We can follow the stack all through this: DOUBLE dose — . Prints 46
On balance, then DOUBLE takes a number off the stack, and it's important to
realise that FORTH words are quite entitled to do this. A word takes some numbers
off the stack (these are its operands, the numbers it operates on) and leaves some on
the stack when it has finished (these are its results), but there is nothing to say that
the number of operands must match the number of results. (Of course, the actual cards will have numbers written on them instead of K and Q;
but I don't know what the numbers are going to be so I've written K and Q instead.) DROP takes one number off the top of the stack and throws it away, changing
But it only takes off one number, so for two or more it changes Here is a word SQ that works out the square of a number (the number multiplied by itself). It doesn't print out the answer, so it has one operand (the original number) and one result (its square), changing ↑ ↑ operand cardresult card Again, in real life the operand card will have a number written on it instead of K; and 'K*K' is just a symbolic way of showing that the result will be that number multiplied by itself. The definition of SQ is : SQ DUP * ; which you can test with examples like 6 SQ . (Work out how the stack changes as SQ is obeyed.) Rather than drawing pictures of cards all the time, we shall use a notation that sets it all on one line, replacing the card diagram by the line (K — K*K) ↑ ↑ operandresult If a word has more than one operand or result then we list them all. For instance, for /MOD (K,Q - remainder of K ÷ Q, quotient of K ÷ Q) ↑ ↑ ↑ ↑ operandtop operand result top result second from topon stack second from top on stack When listing either the operands or the results, the top of the stack comes last. In cards, the change is from lowest is listed first top is listed last It's essential to know exactly what operands each word expects to find on the stack, and what results it leaves at the end, so it's a good idea to build this information into the word definition itself. You do this using comments — anything enclosed in round brackets is a comment, there purely for your benefit, and ignored by the computer when it executes the word. Here is a definition of SQ that uses a comment to show how SQ affects the stack. : SQ The computer ignores You can put comments in anywhere between the name of the new word and the
semicolon, and they don't have to describe the stack — they can say anything you like
to help you remember what you meant when you defined the word. The first round
bracket, (, needs a space after it because it is itself a FORTH word (meaning 'here
comes a comment'). Remember that you can't have a ) actually inside the comment,
because it means 'end of comment'. DUP (K — K,K)
duplicates the top number on the stack. PICK (n — K) takes a number (we have written n for it) off the top, and makes a copy of the nth one down from the top of the stack in what remains, leaving this copy on the top. For instance, PICK changes
because J is the third one down in ROLL (n — ) takes a number n off the top of the stack; and then, in what is left, rotates the top n numbers, bringing the nth to the top. For instance, ROLL changes by rotating the four cards Exercises 1. Define a word to take a price including VAT off the stack, and return as result the VAT paid. (See Exercise 1 in the previous chapter. Check that it gives the right answer for £89.95.) 2. Convince yourself that the following are true:
1 PICK is the same as DUP [Top] | [Back] | [Next] |