home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 4 Part 050 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
- ┌──────────────────────────────┐
- │ Using Variables in Forth │
- └──────────────────────────────┘
-
- If you recall, we had just introduced variables. We have deliberately
- delayed the introduction of variables so as to give you plenty of time
- to become experienced with Forth's parameter stack. The stack is
- Forth's way of passing information from one procedure or word to another
- and it is also the preferred way for words or procedure to exchange
- information. One mistake new Forthers often make, especially those with
- knowledge of BASIC is to make use of variables to exchange information
- when it would be more appropriate to use the stack.
-
- So the question then is the following: When is it appropriate to use
- variables? There are a number of instances when the use of a variable
- is appropriate.
-
- 1) You are building a system where the value of a quantity must be
- referred to by a large number of different words or procedures. This
- quantity may seldom change but you want the option to change its value
- under certain circumstances.
-
- Many examples of this type of variable use can be found in Forth. You
- may recall that earlier on we wrote the definition of the word BINARY to
- force Forth to work with a number base or radix of 2. The definition
- was:
- : BINARY 2 BASE ! ;
-
- The word BASE is a variable used by your Forth system, especially the
- words for inputting and outputting numbers. You can change the value of
- this variable an Forth will do all arithmetic and numeric I/O in the new
- number base.
-
- 2) You are building a system where the value of some quantity must be
- maintained and changed occasionally. This value may not be needed very
- often so it would be inappropriate to keep it on the stack.
-
- An example of this type of variable usage was seen in the simple random
- number generator that we used earlier.
-
- \ The initial value of SEED could be set by the program that uses RND
- VARIABLE SEED 1234 SEED !
-
- \ Generate random number r between 0 and 65535
- : (RND) ( -- r )
- SEED @ 259 * 3 + 32767 AND DUP SEED ! ;
-
- \ Generate random number r between 0 and n
- : RND ( n -- r ) \ r is a random number 0 <= r < n
- (RND) 32767 */ ;
-
- Note that the variable SEED is only referenced by the word RND but must
- be maintained between successive and possibly infrequent calls to RND.
-
- In contrast the variable BASE is referred to by a host of words in the
- Forth System and programmers often use the knowledge of this variables
- function when writing their own utilities.
-
- ╓─────────────╖
- ║ Problem 4.4 ║
- ╙─────────────╜
- Consider the definition of the word DICE below which results in two
- numbers between 1 and 6 being left on the stack to represent the tossing
- of two dice.
-
- : DICE ( -- die1 die2 )
- 6 RND 1+ 6 RND 1+ ;
-
- Create three variables called UNDER_SEVEN SEVEN and OVER_SEVEN
-
- Write a word called TRIALS which when executed with a number on the
- parameter stack: n TRIALS will execute DICE n times and then keep
- track of the number of times the some of the points on the two dice was
- under 7 , equal to 7 or over 7 in the appropriate variables. Your word
- should also print out a little report when it is finished stating the
- number of trials performed and the number of times each of the three
- possibilities occurred.
-
- ╓──────────────╖
- ║ Problem 4.5 ║
- ╙──────────────╜
- Write the word CARD described below. CARD draws one card from a
- standard deck of 52 cards. When CARD is executed it will leave the suit
- as a number 1-4 and the face value as 1-13. ie CARD ( -- suit value )
- Assume 1 is Heart, 2 is Diamond, 3 is Club and 4 is Spade. Write a word
- similar to the one above that will perform n draws from a deck of 52
- cards (with the card being replaced each time!) and keep track of the
- number of times each suit occurs, and the number of times a face card is
- drawn. Your word should also print out a report when it is finished.
-
- ╓──────────────╖
- ║ Problem 4.6 ║
- ╙──────────────╜
- Modify Problem 3.30 which kept calculated the area of a triangle using
- Hero's formula so that the sides of the triangle are kept in the
- variables called A B and C. Compare the two programs and comment on
- which one you like best.
-
- Next time we continue with a look at Forth CONSTANTs.
-
- ┌────────────────────────────────────┐
- │ Please move to Lesson 4 Part 060 │
- └────────────────────────────────────┘
-