home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l3p120
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
119 lines
╔════════════════════════════════════════════════════╗
║ Lesson 3 Part 120 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
We begin with a problem.
╓──────────────╖
║ Problem 3.26 ║
╙──────────────╜
Modify the number guessing game of problem 3.25 so that it keeps track
of the number of guesses required and reports this at the end of the
game. You should keep track of the number of guesses as the 4th item on
the stack. The stack for the new version of the game will look like:
( guess# secret# old# new# )
You could use this word to increment the guess#
: GUESS+1 ( guess# secret# old# new# -- guess#+1 secret# old# new#)
2SWAP SWAP 1+ SWAP 2SWAP ;
Good Luck. This is a hard problem!
╓──────────────╖
║ 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.
\ 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 ;
┌────────────────────┐
│ The Return Stack │
└────────────────────┘
We mentioned earlier that Forth had two stacks. The one we have been
using of all of our word so far is called the parameter stack. Forth's
other stack which is usually kept secret from beginners is called the
" return stack" The return stack is used by your Forth system to keep
track of the information required to pass control from one Forth word to
another. It is also used to keep information required when DO ...
LOOPS are executing. The return stack is intended for use only by the
Forth system and any programmers that muck with it can expect trouble to
come their way sooner or later. Now we know that you would probably
experiment with the return stack especially if you were told that it was
dangerous so here are some guidelines to follow.
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.
┌──────────────────────┐
│ Return 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.
: AVERAGE ( x1 x2 ... xn -- avg )
DEPTH >R R@ 1- 0
?DO + LOOP
CR ." The average of the " R@ . ." numbers is "
R> / . CR ;
╓───────────────╖
║ Problem 3.28 ║
╙───────────────╜
Rewrite AVERAGE so that it takes number pairs, class mark xi
and frequency fi . ie average = [ sum xi*fi ]/n n = sum fi
AVERAGE ( x1 f1 x2 f2 ... xk fk -- )
╓───────────────╖
║ 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.
: 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.
┌────────────────────────────────────┐
│ Please move to Lesson 3 Part 130 │
└────────────────────────────────────┘