home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l3p050
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
118 lines
╔════════════════════════════════════════════════════╗
║ Lesson 3 Part 050 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌────────────────────────────┐
│ Interval Testing Words │
└────────────────────────────┘
We continue our discussion of flags and conditionals with some of my
favorite words. Interval testing words. Naming convention motivated by
the mathematical intervals (a,b) a < x < b , [a,b] a <= x <= b (a,b]
a < x <= b and [a,b) a <= x < b.
\ (IN) leaves a true flag if a < x < b
: (IN) ( x a b -- flag ) ( JWB 28 09 88 )
\ Remove the comment from the line below for run time error checking.
\ 2DUP < NOT ABORT" Invalid interval."
-ROT OVER < -ROT > AND ;
\ [IN] leaves a true flag if a <= x <= b , otherwise false.
: [IN] ( x a b -- flag ) 1+ SWAP 1- SWAP (IN) ;
\ (IN] leaves a true flag if a < x <= b , otherwise false.
: (IN] ( x a b -- flag ) 1+ (IN) ;
\ [IN) leaves a true flag if a <= x < b , otherwise false.
: [IN) ( x a b -- flag ) SWAP 1- SWAP (IN) ;
Here are the results of throughly testing [IN]
2 3 5 [IN] . <enter> 0 ok \ False because 2 is not in [3,5]
3 3 5 [IN] . <enter> -1 ok \ True because 3 is in [3,5]
4 3 5 [IN] . <enter> -1 ok \ True because 4 is in [3,5]
5 3 5 [IN] . <enter> -1 ok \ True because 5 is in [3,5]
6 3 5 [IN] . <enter> 0 ok \ False because 6 is not in [3,5]
╓──────────────╖
║ Problem 3.10 ║
╙──────────────╜
Construct test data to verify the operation of (IN] (IN) and [IN).
For an interval to be valid we must have a < b. What happens when the
interval testing words are used with invalid intervals? Modify the
definition of (IN) as suggested and try using an invalid interval.
╓──────────────╖
║ Problem 3.11 ║
╙──────────────╜
Write definitions of [IN] (IN] and [IN) that do not use (IN) or
each other!
Using Forth's Conditional Programming Structures.
IF ... THEN and IF ... ELSE ... THEN
CONDITIONAL STRUCTURES CAN ONLY BE USED WITHIN A COLON DEFINITIONS.
<condition> IF <do this part only if condition is true>
THEN <continue here>
<condition> IF <do this part only if condition is true>
ELSE <do this part only if condition is false>
THEN <continue here>
The best way to learn about these is to study some simple programs.
Here is a word that will test the top stack number to determine whether
or not it is odd or even. A message is printed to announce the result
of the test.
: TEST ( n -- ) \ Determine if number is even or odd.
CR DUP ." THE NUMBER " . ." IS AN "
DUP 2/ 2* = \ Test if number is even.
IF ." EVEN "
ELSE ." ODD "
THEN ." NUMBER" ;
╓───────────────╖
║ Problem 3.12 ║
╙───────────────╜
Use the word TEST on some even and odd numbers to make sure it works as
advertised! The second line of TEST ( the one that tests if the number
is even or odd ) is a little clumsy. Find a better way to test if a
number is even. We know of at least two better ways to test for
evenness. How many ways can you find?
╓──────────────╖
║ Problem 3.13 ║
╙──────────────╜
Write word similar to TEST , whose output is a sentence stating whether
the top number on the stack is positive , zero or negative.
╓──────────────╖
║ Problem 3.14 ║
╙──────────────╜
Write a word called EVEN ( n flag ) , that takes a stack input n
and leaves a true flag if n is even and a false flag if n is odd.
Two new words: KEY and EXIT
KEY ( -- n ) Wait for user to press key on keyboard and
return the key code n.
EXIT ( -- ) When compiled in a word, EXIT , will cause
termination of word execution when encountered.
: KEY_TEST ( -- ) \ Using Exit to exit an infinite loop.
BEGIN CR KEY
DUP CONTROL M = \ Control M is return key.
IF DROP EXIT THEN \ Exit infinite loop if pressed.
DUP . EMIT AGAIN ; \ Otherwise show key pressed.
╓───────────────╖
║ Problem 3.15 ║
╙───────────────╜
Run this word and describe what happens when the function keys
are pressed.
┌────────────────────────────────────┐
│ Please move to Lesson 3 Part 060 │
└────────────────────────────────────┘