home *** CD-ROM | disk | FTP | other *** search
- \ Lesson 3 Part 5 ( F-PC 3.5 Tutorial by Jack Brown )
-
- COMMENT:
- 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.
- COMMENT;
-
- \ (IN) leaves a true flag if a < x < b
- : (IN) ( x a b -- flag ) ( JWB 28 09 88 )
- 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) ;
-
-
- : 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" ;
-
- COMMENT:
- 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.
- COMMENT;
-
- : 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.
-
-