Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!bouma From: bouma@cs.purdue.EDU (William J. Bouma) Newsgroups: comp.lang.forth Subject: Re: Recursion (was Re: The most fantastically fun word...) Summary: handling recursion in FORTH Message-ID: <6730@medusa.cs.purdue.edu> Date: 9 May 89 20:40:35 GMT References: <6716@medusa.cs.purdue.edu> <5096@tekgvs.LABS.TEK.COM> Distribution: usa Organization: Department of Computer Science, Purdue University Lines: 41 In article <5096@tekgvs.LABS.TEK.COM> toma@tekgvs.LABS.TEK.COM (Tom Almy) writes: >In article <6716@medusa.cs.purdue.edu> bouma@cs.purdue.EDU (William J. Bouma) writes: >>standard. It was really stupid , since all I had to do to make recursion >>work in the thing was to put the word being defined into the dictionary as >>soon as it was seen, rather than after the definition was complete. The > > >There is a good reason *not* to make a word be visible in the dictionary >before it is seen -- sometimes it is nice to be able to redefine a >word in terms of itself. As an example: > >: / DUP 0= IF ." ATTEMPT TO DIVIDE " DROP . ." BY ZERO" ABORT THEN / ; > I do not agree with this reasoning. It seems like redefining a word in terms of itself is a more unusual case than is recursion. Thus it should have to suffer the unnatural syntax rather than the recursion. In my system I do it like this: : / DUP 0= IF ." ATTEMPT TO DIVIDE " DROP . ." BY ZERO" ABORT THEN '' / ; where '' just grabs the next word and gets its old definition. Of course either way will work, but I see no "good reason" not to do it my way. >There is a nice feature in Laboratory Microsystem's Forths (and others) >that allows forward referencing of colon definitions and thus mutually >recursive words. It is used in this way: > >F: WORD-B >: WORD-A ... WORD-B ... ; >R: WORD-B ... WORD-A ... ; Durring a colon definition, I just put all undefined words in the dictionary with their code fields pointing at code that traps an error and prints " undefined word executed WORD ". This eliminates the need for any special mechanism for mutual recursion. When WORD-B is finaly defined, the code pointer is just changed to point at the current code (as any normal re- definition). -- Bill || ...!purdue!bouma