home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l4p050 < prev    next >
Text File  |  1990-07-15  |  5KB  |  108 lines

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 4 Part 050  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.                ┌──────────────────────────────┐
  6.                │   Using  Variables in Forth  │
  7.                └──────────────────────────────┘
  8.  
  9. If you recall, we had just introduced variables.  We have deliberately
  10. delayed the introduction of variables so as to give you plenty of time
  11. to become experienced with Forth's parameter stack.  The stack is
  12. Forth's way of passing information from one procedure or word to another
  13. and it is also the preferred way for words or procedure to exchange
  14. information.  One mistake new Forthers often make, especially those with
  15. knowledge of BASIC is to make use of variables to exchange information
  16. when it would be more appropriate to use the stack.
  17.  
  18. So the question then is the following:  When is it appropriate to use
  19. variables?  There are a number of instances when the use of a variable
  20. is appropriate.
  21.  
  22. 1) You are building a system where the value of a quantity must be
  23. referred to by a large number of different words or procedures.  This
  24. quantity may seldom change but you want the option to change its value
  25. under certain circumstances.
  26.  
  27. Many examples of this type of variable use can be found in Forth.  You
  28. may recall that earlier on we wrote the definition of the word BINARY to
  29. force Forth to work with a number base or radix of 2.  The definition
  30. was:
  31.           : BINARY  2 BASE ! ;
  32.  
  33. The word BASE is a variable used by your Forth system, especially the
  34. words for inputting and outputting numbers.  You can change the value of
  35. this variable an Forth will do all arithmetic and numeric I/O in the new
  36. number base.
  37.  
  38. 2) You are building a system where the value of some quantity must be
  39. maintained and changed occasionally.  This value may not be needed very
  40. often so it would be inappropriate to keep it on the stack.
  41.  
  42. An example of this type of variable usage was seen in the simple random
  43. number generator that we used earlier.
  44.  
  45. \ The initial value of SEED could be set by the program that uses RND
  46.   VARIABLE SEED    1234  SEED  !
  47.  
  48. \ Generate random number r between 0 and 65535
  49. : (RND)  ( -- r )
  50.     SEED @ 259 * 3 + 32767 AND DUP SEED ! ;
  51.  
  52. \ Generate random number r between 0 and n
  53. : RND  ( n -- r )   \ r is a random number   0 <= r < n
  54.         (RND) 32767 */ ;
  55.  
  56. Note that the variable SEED is only referenced by the word RND but must
  57. be maintained between successive and possibly infrequent calls to RND.
  58.  
  59. In contrast the variable BASE is referred to by a host of words in the
  60. Forth System and programmers often use the knowledge of this variables
  61. function when writing their own utilities.
  62.  
  63. ╓─────────────╖
  64. ║ Problem 4.4 ║
  65. ╙─────────────╜
  66. Consider the definition of the word DICE below which results in two
  67. numbers between 1 and 6 being left on the stack to represent the tossing
  68. of two dice.
  69.  
  70. : DICE  ( --  die1  die2 )
  71.         6 RND 1+  6 RND 1+  ;
  72.  
  73. Create three variables called UNDER_SEVEN  SEVEN and OVER_SEVEN
  74.  
  75. Write a word called TRIALS which when executed with a number on the
  76. parameter stack:   n TRIALS   will execute DICE n times and then keep
  77. track of the number of times the some of the points on the two dice was
  78. under 7 , equal to 7 or over 7 in the appropriate variables.  Your word
  79. should also print out a little report when it is finished stating the
  80. number of trials performed and the number of times each of the three
  81. possibilities occurred.
  82.  
  83. ╓──────────────╖
  84. ║ Problem 4.5  ║
  85. ╙──────────────╜
  86. Write the word CARD described below. CARD  draws one card from a
  87. standard deck of 52 cards. When CARD is executed it will leave the suit
  88. as a number 1-4 and the face value as 1-13. ie  CARD  ( -- suit value )
  89. Assume 1 is Heart, 2 is Diamond, 3 is Club and 4 is Spade.  Write a word
  90. similar to the one above that will perform   n  draws from a deck of 52
  91. cards (with the card being replaced each time!) and keep track of the
  92. number of times each suit occurs, and the number of times a face card is
  93. drawn.  Your word should also print out a report when it is finished.
  94.  
  95. ╓──────────────╖
  96. ║ Problem 4.6  ║
  97. ╙──────────────╜
  98. Modify Problem 3.30 which kept calculated the area of a triangle using
  99. Hero's formula so that the sides of the triangle are kept in the
  100. variables called  A  B  and  C.  Compare the two programs and comment on
  101. which one you like best.
  102.  
  103. Next time we continue with a look at Forth CONSTANTs.
  104.  
  105. ┌────────────────────────────────────┐
  106. │  Please move to Lesson 4 Part 060  │
  107. └────────────────────────────────────┘
  108.