home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tile-forth-2.1-base.tgz / tile-forth-2.1-base.tar / fsf / tile-forth / tst / fibonacci.tst < prev    next >
Text File  |  1991-09-14  |  912b  |  40 lines

  1. .( Loading Fibonacci benchmark...) cr
  2.  
  3. \ This is a standard benchmark used for interpretive languages such as
  4. \ Lisp. Computation of Fibonacci for 20 in MacLisp interpreter on a
  5. \ DEC KA10 takes about 3.6 minutes. The Scheme-79 chip was reported
  6. \ by G.J. Sussman et al. in IEEE COMPUTER, July 1981, to perform
  7. \ the task in about a minute at 1595 ns clock period and 32K Lisp cells.
  8. \
  9. \ The recursive and tail recursive versions in forth. Demonstrates
  10. \ the power of forth (**4).
  11.  
  12. : fib ( n -- m)
  13.   dup 1 >
  14.   if dup 1- recurse
  15.     swap 2- recurse +
  16.   then
  17. ;
  18.   
  19. : recursive-fib ( -- )
  20.   20 fib 6765 = not abort" recursive-fib: wrong result"
  21. ;
  22.  
  23. : fib-tail ( a b c -- m)
  24.   ?dup
  25.   if 1- -rot over + swap rot tail-recurse
  26.   else nip then
  27. ;
  28.  
  29. : fib-iter ( n -- m) 1 0 rot fib-tail ;
  30.  
  31. : tail-recursive-fib ( -- )
  32.   1000 0 do
  33.     20 fib-iter
  34.     6765 = not abort" tail-recursive-fib: wrong result"
  35.   loop
  36. ;
  37.  
  38. forth only
  39.  
  40.