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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 3 Part 120  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.  
  6. We begin with a problem.
  7.  
  8. ╓──────────────╖
  9. ║ Problem 3.26 ║
  10. ╙──────────────╜
  11. Modify the number guessing game of problem 3.25 so that it keeps track
  12. of the number of guesses required and reports this at the end of the
  13. game.  You should keep track of the number of guesses as the 4th item on
  14. the stack.  The stack for the new version of the game will look like:
  15.  
  16. ( guess# secret# old# new# )
  17.  
  18. You could use this word to increment the guess#
  19.  
  20. : GUESS+1 ( guess# secret# old# new# -- guess#+1 secret# old# new#)
  21.         2SWAP SWAP 1+ SWAP 2SWAP ;
  22.  
  23. Good Luck.  This is a hard problem!
  24.  
  25. ╓──────────────╖
  26. ║ Problem 3.27 ║
  27. ╙──────────────╜
  28. Consider the following silly little program.  Study the program to see
  29. how it works. Then expand this silly game to give more and better
  30. responses.
  31.  
  32. \ A nasty game for the IBM-PC .
  33. : WHITE  177 EMIT ;
  34. : GAME  CR
  35.         CR  ." Press the space bar as hard as you can!"
  36.         BEGIN CR
  37.         KEY DROP CR 64 RND 1+
  38.         DUP 0 ?DO WHITE LOOP CR
  39.         DUP 25 < IF ." Press it harder!!" ELSE
  40.         DUP 50 < IF ." Not bad! Press real hard!" ELSE
  41.         10 0 DO BEEP LOOP
  42.         DROP ." You just busted your space bar!"
  43.         EXIT THEN THEN
  44.         DROP AGAIN  ;
  45.  
  46.              ┌────────────────────┐
  47.              │  The Return Stack  │
  48.              └────────────────────┘
  49.  
  50. We mentioned earlier that Forth had two stacks.  The one we have been
  51. using of all of our word so far is called the parameter stack.  Forth's
  52. other stack which is usually kept secret from beginners is called the
  53. " return stack"   The return stack is used by your Forth system to keep
  54. track of the information required to pass control from one Forth word to
  55. another.  It is also used to keep information required when  DO ...
  56. LOOPS are executing.  The return stack is intended for use only by the
  57. Forth system and any programmers that muck with it can expect trouble to
  58. come their way sooner or later.  Now we know that you would probably
  59. experiment with the return stack especially if you were told that it was
  60. dangerous so here are some guidelines to follow.
  61.  
  62.  
  63. New Words:  >R  R>  and  R@  for accessing the return stack. These words
  64. are very dangerous!! Do NOT test or execute them interactively if you do
  65. you will crash the Forth system. They can only be used within colon
  66. definitions.
  67.  
  68.  
  69. >R  ( n   -- ) Transfer top data stack item to return stack.
  70. R>  ( --   n ) Transfer top return stack item to data stack.
  71. R@  ( --   n ) Copy     top return stack item to data stack.
  72.  
  73.               ┌──────────────────────┐
  74.               │  Return Stack Rules  │
  75.               └──────────────────────┘
  76.  
  77. 1. Each use of >R must be balanced with a corresponding R>.
  78. 2. Do not use >R R> and R@ within DO ... LOOPs.  Loop control
  79.    info is kept on the return stack and could be destroyed.
  80.  
  81. Here is a sample program that uses the return stack. The DEPTH of
  82. the stack which is equal to the count of numbers to be averaged is
  83. tucked safely away on the return stack until it is needed.
  84.  
  85. : AVERAGE  ( x1 x2 ... xn --  avg )
  86.       DEPTH >R R@ 1- 0
  87.         ?DO + LOOP
  88.         CR ." The average of the " R@ . ." numbers is "
  89.         R> / .  CR ;
  90.  
  91. ╓───────────────╖
  92. ║ Problem 3.28  ║
  93. ╙───────────────╜
  94. Rewrite AVERAGE  so that it takes number pairs, class mark xi
  95. and frequency fi .  ie average = [ sum xi*fi ]/n   n = sum fi
  96.  
  97.  AVERAGE ( x1 f1 x2 f2 ... xk  fk    -- )
  98.  
  99. ╓───────────────╖
  100. ║ Problem 3.29  ║
  101. ╙───────────────╜
  102. Consider the following simple program that produces a simple bar chart
  103. or histogram from a list of frequencies that are placed on the parameter
  104. stack.
  105.  
  106. : WHITE  177 EMIT ;
  107. \ Given n frequencies construct histogram or bar chart.
  108. : HISTOGRAM ( f1 f2 ... fn   -- )
  109.         CR DEPTH 0
  110.         ?DO  CR DUP 0 ?DO WHITE LOOP  SPACE .  LOOP CR ;
  111.  
  112. Modify HISTOGRAM so that the bars come out in the proper order
  113. ( f1 first). Hint: " ROLL "  the stack and display bar.  Clean
  114. the stack when finished printing bars.
  115.  
  116. ┌────────────────────────────────────┐
  117. │  Please move to Lesson 3 Part 130  │
  118. └────────────────────────────────────┘
  119.