OSDN Git Service

Add: support namespace
[happyabc/happyabc.git] / example / rec.scm
1 ;; expected output
2 ;;;  not rec
3 ;;;  rec
4 ;;;  3628800
5 ;;;  3628800
6
7 ;; check if not recursion
8 (let ([f (lambda (n) (print "not rec"))])
9   (let ([f (lambda (n) (if (= n 0)
10                            (print "rec")
11                            (f 0)))])
12     (f 1)))
13
14 (let ([f (lambda (n) (print "not rec"))])
15   (letrec ([f (lambda (n) (if (= n 0)
16                               (print "rec")
17                               (f 0)))])
18     (f 1)))
19
20 ;; check if recursion
21 (letrec ([fact (lambda (n) 
22                  (if (<= n 1)
23                      1
24                      (* n (fact (- n 1)))))])
25   (print (fact 10)))
26
27 ;; check if define
28 (define (fact n)
29   (if (<= n 1)
30       1
31       (* n (fact (- n 1)))))
32 (print (fact 10))