home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l4p100
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
111 lines
╔════════════════════════════════════════════════════╗
║ Lesson 4 Part 100 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌─────────────────────────────────┐
│ The Marbles Example Revisited │
└─────────────────────────────────┘
The definition below has been reformated and commented to make it more
readable. It was originally coded in an old fashioned Forth screen or
block of 16 lines by 64 characters and it was not changed when it was
first presented at the end of L4P080.
: MARBLES ( {n} color -- ) \ There may be 1 or 2 numbers on stack.
TABLE + \ Add colour offset to TABLE
DEPTH 1 = \ Check for {n} present.
IF SHOW THEN \ Set display mode if no {n}
LESS? IF SWAP NEGATE SWAP +! \ Do the subtract function.
ELSE SHOW?
IF @ . \ Do the display function.
ELSE MORE?
IF +! \ Do the add function.
ELSE ! \ Do the reset function.
THEN
THEN
THEN
ONLY ; \ Set the default mode to the reset function.
Note that the ONLY or reset value function is assumed and not checked
for if none of the other cases are present.
╓──────────────╖
║ Problem 4.13 ║
╙──────────────╜
Incorporate the defintion of ONLY? suggested in Problem 4.12 into the
definiton of MARBLES. Add error checking to make sure that if an
invalid mode is detected an appropriate error message is displayed.
Test it with the following:
: SMASH 5 MODE ! ; <enter> ok
SMASH 10 RED MARBLES <enter> Error: Undefined function. ok
SMASH RED MARBLES <enter> ... what happens and why? (Don't guess!)
Let's look at another array example.
\ The values of DATA are unitialized.
CREATE DATA 20 ALLOT \ Create an array called DATA to hold
\ 10 16-bit numbers at offsets 0, 2, ...18
\ Fetch the ith element of the array DATA
\ Here i can be 0 1 2 3 4 5 6 7 8 or 9
\ If negative or values larger than 9 are used you will
\ fetch non existant array elements.
: DATA@ ( i -- n ) 2* DATA + @ ;
\ Store the ith element of the array DATA
\ WARNING! if values less than 0 or larger than 9 are used
\ you will mutilate the Forth system by overwriting previous
\ of following word definitions or data structures.
: DATA! ( n i -- ) 2* DATA + ! ;
╓──────────────╖
║ Problem 4.14 ║
╙──────────────╜
Use the MOD operator to write versions of DATA@ and DATA! that will
always result in an index of 0 ... 9 being generated with no error
message being generated.
╓──────────────╖
║ Problem 4.15 ║
╙──────────────╜
Use your word [IN] to write versions of DATA@ and DATA! that will issue
an error messages if an index that is not between 0 and 9 is used.
\ Here is a word that clears all the elements of the array DATA.
: CLEAR-DATA ( -- ) 10 0 DO 0 I DATA! LOOP ;
╓──────────────╖
║ Problem 4.16 ║
╙──────────────╜
Write two new versions of CLEAR-DATA , one using FILL and another
using ERASE . Which of the CLEAR-DATA's do you think is the fastest?
\ The following word prompts the user to enter 10 numbers
\ which will be stored in the DATA array as elements 0 ... 9
: GET-DATA ( -- )
10 0 DO CR I 3 .R SPACE #IN I DATA! LOOP ;
\ This word will display the contents of the DATA array
\ along with the appropriate indices.
: SHOW-DATA
10 0 DO CR ." DATA( " I . ." ) ="
I DATA@ 10 .R
LOOP ;
╓───────────────╖
║ Problem 4.17 ║
╙───────────────╜
a) Write a word COUNT-DATA ( -- k ) that leaves the number of
non zero items k in the array DATA on the stack.
b) Write SUM-DATA ( -- sum ) that sums the non zero data values.
c) Write AVERAGE-DATA ( -- ) prints average of the non 0 values.
Be sure to test you words.
┌───────────────────────────────────┐
│ Please move to Lesson 4 Part 110 │
└───────────────────────────────────┘