
An answer for every exercise in the course, each of them run on the machine except the three for S28, which say so. Yours does not have to match. If it does what the exercise asked, it is right.
Try the exercise first. A solution you have read is a great deal less use than one you have fought for.
3.1
." MY NAME IS GAVIN"
3.2
." HELLO " ." THERE"
prints HELLO THEREok. ." adds nothing of its own, so the gap goes inside the quotes, after HELLO. The space straight after each ." does not count: that one belongs to SuperForth.
3.3 EINSTEIN gets EINSTEIN?. DECIMAL gets ok. Almost any word you invent will do for the first.
4.1
1 2 3 . . .
prints 3 2 1. Last on, first off.
4.2 DEPTH . prints how many, without touching them. (DEPTH puts the count on top, and . takes it straight off again.)
4.3
SP! 7 9 .S
5.1 10 20 30 + + . prints 60. The first + adds 20 and 30; the second adds 10 to that.
5.2 60 24 * 7 * . prints 10080.
5.3 60 60 * 24 * . prints 20864. The right answer is 86,400, which is more than 32767, so it wrapped: 86,400 less 65,536 is 20,864. There was no warning.
5.4 10 20 60 + + 3 / . prints 30.
6.1 12 DUP * . prints 144.
6.2 3 DUP DUP * * . prints 27.
6.3 1 2 3 SWAP ROT .S shows 3 2 1. SWAP makes it 1 3 2; ROT brings the 1 to the top.
6.4 1 2 OVER OVER .S shows 1 2 1 2.
7.1 : DOUBLE DUP + ;
7.2 : AVERAGE + 2 / ;
7.3 : GREET ." HELLO GAVIN" ;
7.4
: SUM-OF-SQUARES
SQUARE SWAP SQUARE + ;
Square the top number, bring the other one up with SWAP, square that, add. On one line it would be 39 characters, which is legal and leaves no room for error; two lines is the safer habit.
8.1
: TRIANGLE CR STAR CR
STAR STAR CR
STAR STAR STAR CR ;
On one line this is 54 characters, and SuperForth would have read only the tail of it.
8.2
: GREET CR ." HELLO" CR
." GOODBYE" CR ;
Written on one line this is exactly 40 characters, which is the worst case: SuperForth says ok and defines nothing.
8.3
: MARK ;
: ONE 1 . ;
: TWO 2 . ;
FORGET MARK
After which ONE gets ONE? and TWO gets TWO?.
9.1 BIGFORTH SHAPES, then 1 SELECT, type STAR and TRIANGLE from S8 (in the editor a long line is allowed), ESC, FLUSH, 1 LOAD, TRIANGLE.
9.2 Three things: BIGFORTH SHAPES at the 0: prompt, then 1 LOAD, then TRIANGLE.
9.3 .FILE prints the current file's name. So does DIR, at the end of its list.
10.1 STAR stays. ROW needs a fourth STAR, and the top word needs a fourth ROW and a new name:
: STAR ." *" ;
: ROW STAR STAR STAR STAR CR ;
: SQUARE4 CR ROW ROW ROW ROW ;
10.2 One column beyond the space that follows the bad word, the same as in the middle of a line. With QWERTZ alone on line 0, WHERE puts the cursor at Col 8: six letters, a space, and one more.
10.3 Open a blank line at the top of each screen with CTRL and X, type a comment in round brackets, and then 1 5 INDEX, or however many screens you have. INDEX clears the screen before it prints.
11.1
3 VARIABLE LIVES
: LOSE -1 LIVES +! ;
LOSE LIVES ? prints 2.
11.2 : NEW-GAME 0 SCORE ! ; (RESET would be the obvious name, and SuperForth already has one.)
11.3 12 CONSTANT DOZEN and then 3 DOZEN * . prints 36.
11.4 : BONUS SCORE @ 2 * SCORE ! ; Fetch it, double it, store it back.
12.1
: LAUNCH 0 10 DO I . -1 +LOOP
." LIFTOFF!" ;
12.2 : TRIANGLE 6 1 DO CR I STARS LOOP ; Each row is I stars. One line where S8 needed three, and it will draw any size.
12.3 : SUM100 0 101 1 DO I + LOOP . ; prints 5050. The 0 is the running total, sitting on the stack while the loop adds to it.
13.1 : EVEN? 2 MOD 0= ;
13.2 : POSITIVE? 0 > ; gives 1 for 5, and 0 for -5 and for 0.
13.3 : BIG? ABS 1000 > ; Taking the sign off first means one comparison does for both ends.
14.1
: EVEN-ODD 2 MOD 0= IF ." EVEN"
ELSE ." ODD" THEN ;
14.2 : BIGGER 2DUP < IF SWAP THEN DROP ; Copy both; if the top one is the bigger, swap so that it is underneath; then drop the smaller.
14.3
: COUNT-UP -DUP IF 1+ 1 DO I .
LOOP THEN ;
-DUP leaves nothing to loop on when n is 0, and the IF skips the loop altogether.
15.1
: HALVING BEGIN DUP . 2 /
DUP 0= UNTIL DROP ;
100 HALVING prints 100 50 25 12 6 3 1.
15.2
: THREES 1 BEGIN DUP 1000 <
WHILE DUP . 3 * REPEAT DROP ;
prints 1 3 9 27 81 243 729.
15.3
: PAUSE ." PRESS ANY KEY" KEY DROP
." THANKS" ;
16.1
: DAY CASE
1 =OF ." MON" ENDOF 2 =OF ." TUE" ENDOF
3 =OF ." WED" ENDOF 4 =OF ." THU" ENDOF
5 =OF ." FRI" ENDOF 6 =OF ." SAT" ENDOF
7 =OF ." SUN" ENDOF ." ???" ENDCASE ;
16.2
: MENU KEY CASE
65 =OF ." APPLE" ENDOF
66 =OF ." BANANA" ENDOF
." ?" ENDCASE ;
16.3
: SEASON CASE
12 =OF ." WINTER" ENDOF
3 <OF ." WINTER" ENDOF
3 5 <OF> ." SPRING" ENDOF
6 8 <OF> ." SUMMER" ENDOF
." AUTUMN" ENDCASE ;
December is caught by a test of its own before anything else, January and February by 3 <OF, and autumn is whatever is left.
17.1 : BINARY 2 BASE ! ; and then 10 BINARY . DECIMAL prints 1010.
17.2 : H. BASE @ SWAP HEX . BASE ! ; Fetch the present base and tuck it under the number; print in hex; put the old base back.
17.3 HEX FF DECIMAL . prints 255. HEX FFFF DECIMAL U. prints 65535, and needs U.: with . it would print -1.
18.1 NEWFILE FLOAT and 1 22 INDEX. Twenty of the twenty-two screens have a comment on their top line; 13 and 20 are blank.
18.2 Start with BIGFORTH MYWORK. Then NEWFILE UTILITY, 1 LOAD, NEWFILE MYWORK, 1 SELECT, and on the screen:
( DICE - NEEDS UTILITY SCREEN 1 )
: DICE 6 CHOOSE 1+ ;
: THROWS 10 0 DO DICE . LOOP ;
ESC, FLUSH, 1 LOAD, THROWS. The comment on line 0 is a note to yourself for next time.
18.3 1 LOAD stops at CHOOSE?, because CHOOSE lived in memory and the restart wiped it. Every session: load the extension first, then your own screens.
19.1
: DOUBLE-IT ." A NUMBER?" CR
#IN 2 * . ;
19.2
: ADD-UP ." FIRST?" CR #IN
." SECOND?" CR #IN + . ;
19.3
: TABLE-FOR ." WHICH TABLE?" CR #IN
11 1 DO DUP I * . LOOP DROP ;
20.1 : ROLLS 0 DO DICE . LOOP ; and then 10 ROLLS.
20.2
: COIN 2 CHOOSE IF ." HEADS"
ELSE ." TAILS" THEN ;
2 CHOOSE is 0 or 1, which is already a flag.
20.3
: SIXES 0 600 0 DO 6 CHOOSE 1+
6 = + LOOP . ;
A hundred is what you would expect; one run gave 96. The trick is 6 = +: the comparison leaves 1 for a six and 0 otherwise, and that is added straight on to the count.
21.1 : TOTAL 0 7 0 DO I TEMPS @ + LOOP ;
21.2
: BIGGEST 0 7 0 DO I TEMPS @ MAX
LOOP ;
It starts from 0, so it would be wrong for a table of negative numbers.
21.3 : GREET ." HELLO " NAME COUNT TYPE ;
21.4 TOTAL 7 / .
22.1 A heart:
24 60 126 255 255 255 102 0 177 SHAPE
22.2 : NORMAL 15 0 TCOL 4 BCOL ;
22.3
: RAINBOW 16 2 DO I 0 TCOL 176 EMIT
LOOP NORMAL ;
23.1
: RINGS CLS 70 10 DO 6 128 96 I I
0 POLY 10 +LOOP ;
23.2
: LINES CLS 6 0 DO 20 I 25 * 20 +
230 OVER I DRAW LOOP ;
I 25 * 20 + works out the height of each line, and OVER copies it to be the y of the far end as well.
23.3 With NEWFILE UTILITY 1 LOAD done as well:
: SPECKLE CLS 200 0 DO 256 CHOOSE
192 CHOOSE PLOT LOOP ;
24.1
: DROP-IT 180 0 DO 120 I ALIEN WAIT
LOOP ;
24.2
: PATROL 240 0 DO I 90 ALIEN WAIT
LOOP 0 240 DO I 90 ALIEN WAIT
-1 +LOOP ;
24.3
120 VARIABLE X
: SHOW X @ 90 ALIEN ;
: STEER SHOW BEGIN KEY DUP CASE
90 =OF -8 X +! ENDOF
88 =OF 8 X +! ENDOF ENDCASE
SHOW 81 = UNTIL ;
90, 88 and 81 are the codes for Z, X and Q. DUP keeps a copy of the key for the 81 = at the end, because ENDCASE throws its copy away.
25.1
: BAR 262 NOTE 294 NOTE 330 NOTE
262 NOTE ;
: FRERE 0 0 0 0 0 1 CHANNEL
BAR BAR 0 VOLA ;
25.2
: SIREN 0 0 0 0 0 1 CHANNEL 12 VOLA
900 300 DO I FREQA 5 +LOOP
300 900 DO I FREQA -5 +LOOP 0 VOLA ;
25.3
: BEEP 0 0 0 0 0 1 CHANNEL FREQA
12 VOLA 2000 0 DO LOOP 0 VOLA ;
Then 200 BEEP inside LOW? and HIGH?, and 1000 BEEP before GOT IT!.
26.1
2. E 0 FCONSTANT TWO
: ROUND-IT TWO F* MYPI F* F. ;
2.5 E 0 ROUND-IT prints 1.57079500 E 1. The 2 has to be an FCONSTANT: E does not work inside a definition.
26.2 After 14 LOAD:
: HYP FDUP F* FSWAP FDUP F* F+
FSQR F. ;
3. E 0 4. E 0 HYP prints 5.00000000 E 0.
26.3
: SINES 91 0 DO I S->F SINE F.
CR 30 +LOOP ;
27.1 Whatever you type most. FLUSH, 1 LOAD, .S and FORGET MARK are good candidates, the first three with GRAPH-ENTER on the end.
27.2 The FUNKEY lines go on a screen like any others. FUNKEYS has to be loaded before that screen, because FUNKEY is a word from it. So a session runs: extensions, then keys, then your program.
27.3 3 FUNKEY " SP! .S with GRAPH-ENTER before the closing quote. It needs GRAPH-ENTER because a key you still have to press ENTER after saves you nothing. A key cannot print CLEARED with .", because the key's own text ends at the first double quote it meets.
These three change the game itself. They are worked out from its source and have not been run, unlike everything else in this appendix - so treat them as a guide to where to look, and keep a copy of the disc.
28.1 Four loops run 6 1 DO: in SETUP, MOVE-TGTS and SHOW-TGTS, and in HITS?, all on screen 8. TGTS is 40 ARRAY, cells 0 to 39, and each target takes four cells starting at four times its number. Targets are numbered from 1, so target 9 uses cells 36 to 39 and is the last that fits: nine, not ten. 10 1 DO in all four. Target 10 would store past the end of the array, which S21 warned you about. Target 0 is not free either: its sprite would be number 4, which the game uses for the explosion.
28.2 In GETMOVE on screen 7, the four 4s become 8s.
28.3 Add : TITLE ... ; on screen 10 before INSTRUCT, and put TITLE at the front of GAME on screen 11. Start BIGFORTH GAME to edit, UNLOCK first, and FORGET GAME-TASK before loading again.
29.1 With 3 9 and STON, by hand: 2DUP gives 3 9 3 9; > gives 3 9 0; the flag is false, so SWAP . prints 3 and leaves 9. It has two faults: it prints the smaller, and it leaves the other number behind. The true branch is no better: with 9 3, . prints the top number, the 3, and leaves the 9.
: LARGER 2DUP > IF DROP . ELSE
SWAP DROP . THEN ;
29.2 30000 30000 + is 60000, which does not fit: it wraps, and the "average" comes out as -2768. Halve each number first, 2 / SWAP 2 / +, and accept that an odd pair loses a half.
29.3 For the guessing game: THINK ( -- ), LOW? ( n -- ), HIGH? ( n -- ), JUDGE ( n -- n ), TURN ( -- n ), GAME ( -- ). Check that they add up: TURN leaves a guess, and GAME uses it up.
30.1 For instance BIGFORTH SOUNDS, 1 LOAD, : TASK ;, SAVE - which reports 64 blocks for that combination - and then at DOS SAVE 64 TUNE4TH.COM.
30.2 Load the extensions the game needs, then the game; SAVE; and at DOS SAVE n GUESS.COM with the number you were given. Whoever has the disc then types GUESS at the 0: prompt and GAME at SuperForth. (This one is a procedure put together from steps the course has run, not a run of its own.)
30.3 That one is yours.