🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.forth
Article #305

Re: Recursion forth

#305
From: luscher
Date: Sun, 04 May 1986 20:00
40 lines
2086 bytes
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). It seems the very first word that
>I (in all innocence) attempted to define was to evaluate the fibinocci
>function (in C):
>
>	fib(n)
>	{
>		if( n==1 || n==2 ) return 1;
>		return fib(n-1) + fib(n-2);
>	}
>

I recognize the Fibbonacci series here but I don't remember any deffinition
that gave a single number for it!  Anyway here is the FORTH code:

: fib ( n ) DUP 3 < IF  DROP 1  ( return 1 for 1 or 2, assumes n>=1  )
                    ELSE  [ SMUDGE ]  ( allow for recursion )
                        DUP 1- fib    ( here is your fib{n-1}  )
                        SWAP 2- fib   ( followed by  fib{n-2}  )
                        +  [ SMUDGE ] ( sum two fib s, end recursability )
                    THEN  ;

For arguments of n=  1   2   3   4   5   6   7   8
this gives results=  1   1   2   3   5   8  12  21

I don't know if this is quite what you wanted, but the RECURSION works fine!

Best wishes in exploring FORTH!


-- 

    ihnp4------\                  Jim Luscher
  harvard-\     \                 Nicolet Instruments / Test Instrument Div
     seismo!uwvax!nicmad!luscher  5225 Verona Rd. Bldg-2
    topaz-/     /                 Madison, Wi  53711-0288
   decvax------/                  (608) 271-3333 x 2274
Technical Details
Message-ID: <680@nicmad.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!uwvax!astroatc!nicmad!luscher
References: <1854@mtgzz.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!uwvax!astroatc!nicmad!luscher
From: luscher@nicmad.UUCP
Newsgroups: net.lang.forth
Subject: Re: Recursion forth
Message-ID: <680@nicmad.UUCP>
Date: Sun, 4-May-86 16:00:54 EDT
Article-I.D.: nicmad.680
Posted: Sun May  4 16:00:54 1986
Date-Received: Tue, 6-May-86 06:50:31 EDT
References: <1854@mtgzz.UUCP>
Reply-To: luscher@nicmad.UUCP (Jim {4thNIC} Luscher)
Organization: Nicolet Instrument Corp. Madison WI
Lines: 40