papierkorb:forth_style
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| papierkorb:forth_style [2025-08-10 21:34] – ↷ Seite von projects:forth_style nach papierkorb:forth_style verschoben mka | papierkorb:forth_style [Unbekanntes Datum] (aktuell) – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | < | ||
| - | PORTED FROM UseNet => | ||
| - | ------ | ||
| - | |||
| - | From: ir230@sdcc6.ucsd.edu (john wavrik) | ||
| - | | ||
| - | | ||
| - | | ||
| - | Date: 3 Aug 90 03:49:06 GMT | ||
| - | | ||
| - | |||
| - | Wil Baden has started a discussion of Forth style. I dug out the | ||
| - | | ||
| - | | ||
| - | so that style matters. It was chosen so that the issue would be coding | ||
| - | | ||
| - | |||
| - | | ||
| - | guess a user's " | ||
| - | number, an integer from 1 to 100. The computer makes a guess | ||
| - | to which the user must respond by typing LOW, HIGH or RIGHT. | ||
| - | This is repeated until the computer guesses the number. | ||
| - | |||
| - | I don't remember what version of Forth we were using when this was | ||
| - | | ||
| - | |||
| - | We used the following words from a string package (all strings are | ||
| - | | ||
| - | |||
| - | | ||
| - | <n> $VARIABLE X allocates storage for a counted string | ||
| - | of length n. When X executes the address of the string is | ||
| - | put on the stack. | ||
| - | |||
| - | | ||
| - | stored as a counted string. The address of the string is | ||
| - | put on the stack. | ||
| - | |||
| - | | ||
| - | The input is stored as a counted string whose address is | ||
| - | put on the stack. | ||
| - | |||
| - | | ||
| - | if $1 comes before $2 in lexicographic order, 1 if it | ||
| - | comes after, and 0 if the strings are equal. | ||
| - | |||
| - | | ||
| - | Typical usage $" HELLO" X $! where X is a string | ||
| - | variable. | ||
| - | |||
| - | Here is a solution to the problem which shows why some people think | ||
| - | Forth is a write-only language: | ||
| - | |||
| - | ( GUESSING GAME PROGRAM | ||
| - | 10 $VARIABLE A | ||
| - | : | ||
| - | | ||
| - | SWAP DROP 0 1 ELSE A $" LOW" $COMPARE 0= IF ROT DROP SWAP 0 1 ELSE A | ||
| - | | ||
| - | PLAY AGAIN (Y OR N)" KEY DUP 78 = IF DROP 1 1 1 1 ELSE 89 = IF 0 1 1 | ||
| - | 1 ELSE 0 THEN THEN UNTIL ELSE CR 0 THEN THEN THEN UNTIL UNTIL UNTIL ; | ||
| - | |||
| - | ( Why spend hundreds of dollars for a program to obfuscate your source | ||
| - | | ||
| - | |||
| - | | ||
| - | | ||
| - | bad style also. It is a transcription to Forth of a program which the | ||
| - | | ||
| - | years back when Pascal was the " | ||
| - | what Wirth had in mind when he invented Pascal -- but it seems typical | ||
| - | of the way many people write Pascal and ' | ||
| - | |||
| - | ( GUESSING GAME PROGRAM | ||
| - | 10 $VARIABLE ANSWER | ||
| - | : GAME BEGIN 0 101 | ||
| - | | ||
| - | CR ." I GUESS " . CR | ||
| - | BEGIN ." IS THAT HIGH, LOW, OR RIGHT " | ||
| - | | ||
| - | | ||
| - | | ||
| - | ELSE ANSWER $" LOW" $COMPARE 0= | ||
| - | | ||
| - | | ||
| - | IF CR ." I GOT IT!!" | ||
| - | DROP | ||
| - | BEGIN CR ." PLAY AGAIN (Y OR N)" | ||
| - | KEY DUP 78 = --> | ||
| - | |||
| - | ( GUESSING GAME PROGRAM | ||
| - | IF DROP 1 1 1 1 | ||
| - | ELSE 89 = | ||
| - | | ||
| - | ELSE 0 | ||
| - | THEN | ||
| - | THEN | ||
| - | UNTIL | ||
| - | ELSE CR 0 | ||
| - | THEN | ||
| - | THEN | ||
| - | THEN | ||
| - | UNTIL | ||
| - | UNTIL | ||
| - | UNTIL ; | ||
| - | |||
| - | [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 | ||
| - | | ||
| - | |||
| - | The guessing game program is logically more complex than it might | ||
| - | first appear. You might want to see what you can do with it before | ||
| - | | ||
| - | |||
| - | | ||
| - | |||
| - | |||
| - | ( GUESSING GAME PROGRAM | ||
| - | | ||
| - | | ||
| - | |||
| - | : GUESS CR ." I guess " | ||
| - | | ||
| - | | ||
| - | | ||
| - | |||
| - | : START 0 LL ! 101 HH ! GUESS ; | ||
| - | |||
| - | : | ||
| - | : | ||
| - | : | ||
| - | |||
| - | In this version, the complexity of nested control structures has been | ||
| - | | ||
| - | the game (LOW and HIGH are not strings decoded by the program -- but | ||
| - | are now Forth words). The low and high bounds and the guess were put | ||
| - | in variables rather than left on the stack: if the user puts in a bad | ||
| - | | ||
| - | clear the stack. Putting the numbers in variables allows the game to | ||
| - | | ||
| - | |||
| - | While I won't claim that this version is the ultimate, I do feel that | ||
| - | | ||
| - | feel this is true of either of the earlier versions. | ||
| - | |||
| - | | ||
| - | |||
| - | To Wil Baden and others who may be interested in this: | ||
| - | |||
| - | I recently was asked by someone why I was using Forth to do work | ||
| - | in Computer Algebra rather than Scheme or a Computer Algebra system | ||
| - | (like MACSYMA, Maple, Mathematica, | ||
| - | | ||
| - | prove my point, I found a computer algebra project for which I have | ||
| - | code written "by the masters" | ||
| - | by David Stoutemyer in MuSIMP -- the implementation language for the | ||
| - | | ||
| - | were explaining to students how to do the project -- so I must assume | ||
| - | they tried to make their work a model of clarity. I think that the | ||
| - | | ||
| - | | ||
| - | and a bit about symbol manipulation. [The comparison is too long to | ||
| - | post, but I would be willing to email it -- and I would welcome | ||
| - | | ||
| - | | ||
| - | |||
| - | I believe that, potentially, | ||
| - | | ||
| - | the barriers are not in limitations of the language. | ||
| - | |||
| - | John J Wavrik | ||
| - | jjwavrik@ucsd.edu | ||
| - | Univ of Calif - San Diego | ||
| - | La Jolla, CA 92093 | ||
| - | ---------- | ||
| - | |||
| - | |||
| - | </ | ||
papierkorb/forth_style.1754854452.txt.gz · Zuletzt geändert: 2025-08-10 21:34 von mka