go-first

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

demo4.th (439B)


      1 ( compute factorial recursively )
      2 ( take x as input, return x! and x as output )
      3 
      4 : fact-help
      5 
      6   dup if
      7     1 -			( leave x-1 on top )
      8     fact-help		( leave x-1, [x-1]! )
      9     1 +			( leave x, [x-1]!, x )
     10     swap over swap	( leave [x-1]!, x, x )
     11     *			( into x!, x )
     12     swap		( into x, x! )
     13   else
     14     1 swap
     15   then
     16 ;
     17 
     18 : fact
     19 
     20   fact-help
     21   drop
     22 
     23 ;
     24 
     25 : demo4
     26   " 4 factorial is: " 4 fact . cr
     27   " 6 factorial is: " 6 fact . cr
     28 ;
     29 
     30 demo4