home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd6.lzh / TST / fibonacci.tst < prev    next >
Text File  |  1989-12-21  |  901b  |  35 lines

  1. .( Loading Fibonacci benchmark...) cr
  2.  
  3. \ This is a standard benckmark used for interpretive languages such as
  4. \ Lisp. Compution 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 **4.
  11.  
  12. : fib ( n -- m)
  13.   dup 1 >
  14.   if dup 1- recurse
  15.     swap 2- recurse +
  16.   then ;
  17.   
  18. : recursive-fib ( -- )
  19.   20 fib 6765 = not abort" recursive-fib: wrong result" ;
  20.  
  21. : fib-tail ( a b c -- m)
  22.   ?dup
  23.   if 1- rot rot over + swap rot tail-recurse
  24.   else swap drop then ;
  25.  
  26. : fib-iter ( n -- m) 1 0 rot fib-tail ;
  27.  
  28. : tail-recursive-fib ( -- )
  29.   1000 0 do
  30.     20 fib-iter
  31.     6765 = not abort" tail-recursive-fib: wrong result"
  32.   loop ;
  33.  
  34. forth only
  35.