-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalg.scm
44 lines (38 loc) · 975 Bytes
/
alg.scm
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
;; Random stuff in Scheme.
;; Linear recursive process.
(define (fac n)
(if (= n 1)
1
(* n (fac (- n 1)))))
(display (fac 1)) (newline)
(display (fac 2)) (newline)
(display (fac 3)) (newline)
(display (fac 4)) (newline)
(display (fac 5)) (newline)
(display (fac 6)) (newline)
(display (fac 7)) (newline)
(define (fib n)
(cond ((= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1)) (fib (- n 2))))))
(display (fib 2)) (newline)
(display (fib 3)) (newline)
(display (fib 4)) (newline)
(display (fib 5)) (newline)
(display (fib 6)) (newline)
(display (fib 7)) (newline)
(display (fib 8)) (newline)
(display (fib 9)) (newline)
(display (fib 10)) (newline)
(display (fib 11)) (newline)
(display (fib 12)) (newline)
(display (fib 13)) (newline)
;; Linear iterative process.
(define (factorial n)
(define (iter product counter)
(if (> counter n)
product
(iter (* counter product)
(+ counter 1))))
(iter 1 1))
(display (factorial 6)) (newline)