[comp.lang.forth] Recursion, Variables and OTHELLO

UNBCIC@BRFAPESP.BITNET (10/19/90)

Date: Thu, 18 Oct 90 17:12:27 GMT
From: mike newman <maytag!watmath!mwnewman@IUVAX.CS.INDIANA.EDU>
Subject: Re: How do I do this in FORTH?
>
>> I'm new to this news group and I'm also relatively new at writing
>> non-trivial Forth programs.  I have written a 3-D Tic-Tac-Toe playing
>> program in 'c' which I am now trying to convert to Forth.  All has
>> been going smoothly so far until attempting to write the minimax
>> routine in Forth.

> Intereseting you should mention this.  I'm doing exactly the same
> thing with an othello playing program.  I haven't gotten very far, as
> I only last week found a un*x forth to run.  The way I think I'll
> handle it is to define a seperate stack for alpha, beta, depth.
> Set it up analogously to the return stack, ie: define words like
> alpha-pop, alpha-push, alpha@  <==>  r>, >r, r@

You don't need this, you have DOES> ! It's DOES>, in my non-important opinion
that makes Forh better than the other languages (and why I hated TCOM for some
time). Well, use this:

: AUTO ( recursions -- ) \ Let's create a local variable for recursion
  CREATE \ Although it's not really local
  1+ CELLS \ And you need to specify the number of recursions you will need
  0 , \ First, we say in wich recursion it is
  ALLOT \ And then we allocate the space needed
  DOES> ( -- w ) \ But what the variable does?
  DUP @ \ It get the recursion number,
  1+ CELL+ \ and add it to the first position of it's buffer
; \ ok?

: new ( cfa -- ) \ But we need say to the variable that we are recursing
  >BODY \ No problems, we get the address of the recursion #
  1 SWAP +! \ And increment that number!
;

: dispose ( cfa -- ) \ And we need to say that we are getting out of a
                     \ recursion
  >BODY \ We do it the same way...
  -1 SWAP +! \ But decrementing.
;

\ And that's all! You can use it...
10 ( recursions ) AUTO X \ Declaring the variable
5 X ! \ Using it normally
: Something \ or putting it in a definition that use recursion
['] X new \ We need to to this, in the start...
( Place code here :-)
['] X dispose \ And in the end.
; \ But that's all
X ? 5 Ok.
\ And you still have the non-recursed value!
\ Send me more questions or the Othello program (I love it, but never got time
\ to write it in Forth... ;-)

> normal variable "@", or a frame pointer approach, since you don't have
> any literal offset to deal with.  Of course this assumes that there
That's true. But I still prefer the frame approach

                              (8-DCS)