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

  1. .( Loading Locals Test...) cr
  2.  
  3. locals
  4.  
  5.  
  6. .( 1: Redefinition of the basic stack operations using argument binding) cr
  7.  
  8. : swap { a b } b a ;
  9. : dup { a } a a ;
  10. : drop { a } ;
  11. : rot { a b c } b c a ;
  12.  
  13. 1 2 .s swap cr
  14. 3   .s dup cr
  15.     .s drop cr
  16.     .s rot cr
  17.     .s cr
  18. drop drop drop 
  19.  
  20. .( 2: Recursive factorial function with argument binding) cr
  21.  
  22. : fac { n }
  23.   n 0>
  24.   if n 1- recurse n *
  25.   else
  26.     1
  27.   then ;
  28.  
  29. 5 fac . cr
  30.  
  31.  
  32. .( 3: Tail recursive factorial function) cr
  33.  
  34. : tail { n a }
  35.   n 0>
  36.   if n 1- n a * tail-recurse
  37.   else
  38.     a
  39.   then ;
  40.     
  41. 5 1 tail . cr
  42.  
  43.  
  44. .( 4: Iterative factorial function with a local variable) cr
  45.  
  46. : iter { n | a }
  47.   1 -> a
  48.   n 1+ 1 do
  49.     i a * -> a
  50.   loop
  51.   a ;
  52.  
  53. 5 iter . cr
  54.  
  55. forth only
  56.  
  57.