home *** CD-ROM | disk | FTP | other *** search
- \ Lesson 3 Part 12 ( F-PC 3.5 Tutorial by Jack Brown )
-
- COMMENT:
- Problem 3.27
- Consider the following silly little program. Study the program to see
- how it works. Then expand this silly game to give more and better
- responses.
- COMMENT;
-
- \ A nasty game for the IBM-PC .
- : WHITE 177 EMIT ;
- : GAME CR
- CR ." Press the space bar as hard as you can!"
- BEGIN CR
- KEY DROP CR 64 RND 1+
- DUP 0 ?DO WHITE LOOP CR
- DUP 25 < IF ." Press it harder!!" ELSE
- DUP 50 < IF ." Not bad! Press real hard!" ELSE
- 10 0 DO BEEP LOOP
- DROP ." You just busted your space bar!"
- EXIT THEN THEN
- DROP AGAIN ;
-
- COMMENT:
- New Words: >R R> and R@ for accessing the return stack. These words
- are very dangerous!! Do NOT test or execute them interactively if you do
- you will crash the Forth system. They can only be used within colon
- definitions.
-
-
- >R ( n -- ) Transfer top data stack item to return stack.
- R> ( -- n ) Transfer top return stack item to data stack.
- R@ ( -- n ) Copy top return stack item to data stack.
-
- RULES:
- 1. Each use of >R must be balanced with a corresponding R>.
- 2. Do not use >R R> and R@ within DO ... LOOPs. Loop control
- info is kept on the return stack and could be destroyed.
-
- Here is a sample program that uses the return stack. The DEPTH of
- the stack which is equal to the count of numbers to be averaged is
- tucked safely away on the return stack until it is needed.
- COMMENT;
-
- : AVERAGE ( x1 x2 ... xn -- avg )
- DEPTH >R R@ 1- 0
- ?DO + LOOP
- CR ." The average of the " R@ . ." numbers is "
- R> / . CR ;
-
- COMMENT:
- Problem 3.29
- Consider the following simple program that produces a simple bar chart
- or histogram from a list of frequencies that are placed on the parameter
- stack.
- COMMENT;
-
- : WHITE 177 EMIT ;
- \ Given n frequencies construct histogram or bar chart.
- : HISTOGRAM ( f1 f2 ... fn -- )
- CR DEPTH 0
- ?DO CR DUP 0 ?DO WHITE LOOP SPACE . LOOP CR ;
-
- \ Modify HISTOGRAM so that the bars come out in the proper order
- \ ( f1 first). Hint: " ROLL " the stack and display bar. Clean
- \ the stack when finished printing bars.
-