aboutsummaryrefslogtreecommitdiff
path: root/buzzard/demo4.th
blob: 3f9a76dfac2838eb8efda50dd280faa8c3a99b12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
( compute factorial recursively )
( take x as input, return x! and x as output )

: fact-help

  dup if
    1 -			( leave x-1 on top )
    fact-help		( leave x-1, [x-1]! )
    1 +			( leave x, [x-1]!, x )
    swap over swap	( leave [x-1]!, x, x )
    *			( into x!, x )
    swap		( into x, x! )
  else
    1 swap
  then
;

: fact

  fact-help
  drop

;

: demo4
  " 4 factorial is: " 4 fact . cr
  " 6 factorial is: " 6 fact . cr
;

demo4