Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!yale!mintaka!mit-eddie!uw-beaver!ubc-cs!alberta!aunro!idacom!rob From: rob@idacom.uucp (Rob Chapman) Newsgroups: comp.lang.forth Subject: Re: Advanced Beginners Message-ID: <1990Feb26.213303.14029@idacom.uucp> Date: 26 Feb 90 21:33:03 GMT Organization: IDACOM Electronics Ltd. Lines: 74 > From: IAN GREEN Read: (N/A) > Can someone write a demo program to outline a nested FOR loop. > Something like the following pseudo code: > for i = 1 to n > for j = 1 to m > dosomething > next > next This translates quite easily with a sprinkling of postfix to: n FOR m FOR dosomething NEXT NEXT In botForth, if n or m are 0 the 'dosomething' will not be executed. If they are nonzero, then 'dosomething' will be executed n m * times as you wanted. Others please correct me if I err. In ANS Forth I believe the same code will execute n 1 + m 1 + * times and if n or m are 0, the 'dosomething' will be executed at least once. To handle the 0 case ?DUP IF...THEN construncts must be used. I'm not sure, but I think RTXForth follows the ANS Forth definition. (aside to the ANS people: isn't it tougher to explain and harder to use this way?) > How about declaring data structures like an array, or a queue. A > stack is automatic but I am unsure how to implement these other > elementary data structures. I have found queues to be a fundamental piece for interconnecting processes. The queue definition and it's operators extracted from botForth are: ( ==== Queues ===== ) QUEUE ( #words -- ) create a queue with #words entries >Q ( n \ queue -- ) append n to end of queue Q> ( queue -- n ) return first value in queue Q ( queue -- n ) get a copy of first value in queue 0Q ( queue -- ) remove all items from a queue Q? ( queue -- flag ) return number of items in a queue : QUEUE ( #words -- ) ( Queues: | >insert | >remove | >end | queue... | ) R SWAP - DUP 0< IF R @ R> - + 2/ EXIT ENDIF R> DROP 2/ ; : >Q ( n \ queue -- ) DUP>R @ !- DUP 4 - R - IF R> ! EXIT ENDIF @ R> ! ; : Q> ( queue -- n ) 2 + DUP>R @ @- DUP 2 - R - IF R> ! EXIT ENDIF @ R> ! ; : Q ( queue -- n ) 2 + @ @ ; : 0Q ( queue -- ) @+ ! ; ( example ) 10 QUEUE dataq ( queue to hold 10 data items ) dataq 0Q 1 dataq >Q 2 dataq >Q 3 dataq >Q dataq Q? . ( emit a 3 ) dataq Q> dataq Q> dataq Q> . . . ( emit 3 2 1 since the stack ) ( reverses the order ) dataq Q? . ( emit a 0 since it's now empty ) The above definitions have been written for speed on the RTX20xx. Again I'm guessing about the Forth that you are using so I've included definitions and explanations for some non-standard but useful words: ( ==== Incrementing/Decrementing memory access ==== ) @+ ( a -- n \ a+ ) fetch a word from address a and increment a by 2 @- ( a -- n \ a- ) fetch a word from address a and decrement a by 2 !- ( n \ a -- a- ) store a word at address a and decrement a by 2 : @+ ( a -- n \ a+ ) DUP @ SWAP 2 + ; : @- ( a -- n \ a- ) DUP @ SWAP 2 - ; : !- ( n \ a -- a- ) TUCK ! 2 - ; Well this is quite a bit already so I'll leave arrays to someone else. rob@idacom " It's so simple, it has to work! "