Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!pt.cs.cmu.edu!dsl.pitt.edu!pitt!willett!ForthNet From: ForthNet@willett.pgh.pa.us (ForthNet articles from GEnie) Newsgroups: comp.lang.forth Subject: PUZZLES AND PROBLEMS Message-ID: <1542.UUL1.3#5129@willett.pgh.pa.us> Date: 17 Aug 90 04:22:05 GMT Organization: String, Scotch tape, and Paperclips. (in Pgh, PA) Lines: 89 Category 3, Topic 35 Message 138 Thu Aug 16, 1990 W.BADEN1 [Wil] at 18:31 PDT JOHN WAVRIK has supplied an example of vintage Forth in the FigForth dialect that defies understanding. John comments: >[I notice in reproducing it that the programmer forgot to deal with the >case in which the user types in something other than HIGH, LOW or >RIGHT.] The technique of manifesting the stack reveals that the programmer did indeed deal with that case. It's still an example of bad Forth coding style, but now the logic can be followed and understood. ( Guessing Game Program 2A ) 10 $VARIABLE Answer : Game BEGIN ( ) 0 101 ( lo hi) BEGIN 2DUP + 2/ ( lo hi guess) CR ." I guess " DUP . CR BEGIN ." Is that high, low, or right?" In$ Answer $! Answer $" HIGH" $compare 0= IF SWAP DROP ( lo hi) 0 1 ELSE ( lo hi guess) Answer $" LOW" $compare 0= IF ROT DROP SWAP ( lo hi) 0 1 ELSE ( lo hi guess) Answer $" RIGHT" $compare 0= IF CR ." I got it." DROP 2DROP ( ) BEGIN ( ) CR ." Play again (Y or N)?" KEY ( c) DUP 78 ( ie "N") = IF DROP ( ) 1 1 1 1 ( again 1 1 1) ELSE (c ) 89 ( ie "Y") = IF ( ) 0 1 1 1 ( again 1 1 1) ELSE ( ) 0 ( 0) THEN THEN ( 0, or again 1 1 1) UNTIL ( again 1 1) ELSE ( lo hi guess) CR 0 ( lo hi guess 0) THEN THEN THEN ( again 1 1, or lo hi 0 1, or lo hi guess 0) UNTIL ( again 1, or lo hi 0) UNTIL ( again) UNTIL ; JOHN's re-writing of the program is a masterful use of Forth, and little can be done to improve upon it. My only cavil is that the stack state is not explicitly given and traced in the definition of "GUESS". And of course, narrative comments should be properly punctuated. ( Guessing Game Program 3A ) VARIABLE Ll VARIABLE Hh ( Low and high for current range. ) VARIABLE Gg ( Current guess. ) : Guess ( -- ) Ll @ Hh @ + 2/ ( midpt: Guess midpoint of range ) DUP Gg ! ( '' and save it.) CR ." I guess " . ( ) ." Is that LOW, HIGH, or RIGHT ?" ; : GAME 0 Ll ! 101 Hh ! Guess ; : HIGH Gg @ Hh ! Guess ; ( Take lower half of range. ) : LOW Gg @ Ll ! Guess ; ( Take upper half of range. ) : RIGHT CR ." Wow, I got it." ; John has shown that one technique for readable Forth is to mop up the stack in every line. He has also shown the expressive power of an interactive language with a workspace or a language with persistent objects. Lisp, APL, ABC, Eiffel and (I'm told) REXX and the Korn shell can do likewise. (Yes, John, I'd like to see your comparison of Forth and the algebra languages.) Procedamus in pace. ----- This message came from GEnie via willett through a semi-automated process. Report problems to: uunet!willett!dwp or dwp@willett.pgh.pa.us