Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!cmcl2!harvard!think!mit-eddie!genrad!decvax!ittatc!dcdwest!sdcsvax!sdcrdcf!usc-oberon!smeagol!jplgodo!steve From: steve@jplgodo.UUCP (Steve Schlaifer x3171 156/224) Newsgroups: net.lang.forth Subject: Re: Recursion forth Message-ID: <777@jplgodo.UUCP> Date: Mon, 5-May-86 13:29:26 EDT Article-I.D.: jplgodo.777 Posted: Mon May 5 13:29:26 1986 Date-Received: Sat, 10-May-86 12:55:21 EDT References: <1854@mtgzz.UUCP> Organization: Jet Propulsion Labs, Pasadena, CA Lines: 39 Summary: create a word called MYSELF In article <1854@mtgzz.UUCP>, bds@mtgzz.UUCP (b.d.szablak) writes: > > I would appreciate it if someone well versed in forth programming > could give this novice hints on coding recursive algorthims (the two Forth > texts I have manage to avoid this topic). > [........] Essentially, all you need for creating a recursive routine is a word that can compile the CFA of the LATEST word into the dictionary. I don't have a FORTH available here at work right now so I can't check the following out but I think it is essentially correct for FIG FORTH. : MYSELF ( compile CFA of LATEST into dictionary ) ?COMP LATEST PFA CFA , ; IMMEDIATE LATEST stacks the NFA of the most recent word in the dictionary whether that word has been finished yet or not. PFA CFA then converts this into the CFA of the most recent word. Finally, , compiles it into the dictionary. MYSELF can then be used as follows: : RECURS ( decrements TOS until it is 0 ) DUP IF 1 - MYSELF THEN ; Tail recursion could be implemented by modifying MYSELF into the following: : TAIL ( tail recursion word ) ?COMP COMPILE R> COMPILE DROP LATEST PFA CFA , ; IMMEDIATE which drops the return address from the return stack before calling the word it gets used in. -- ...smeagol\ Steve Schlaifer ......wlbr->!jplgodo!steve Advance Projects Group, Jet Propulsion Labs ....group3/ 4800 Oak Grove Drive, M/S 156/204 Pasadena, California, 91109 +1 818 354 3171