-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.scm
More file actions
69 lines (56 loc) · 1.03 KB
/
input.scm
File metadata and controls
69 lines (56 loc) · 1.03 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
;; Use '(load "input.scm")' to execute this file.
;; let*
(define (f x)
(let* ((a x)
(b (+ a 2)))
(+ a b)))
(f 1)
;; letrec
(define (f x)
(letrec ((even?
(lambda (n)
(if (= n 0)
#t
(odd? (- n 1)))))
(odd?
(lambda (n)
(if (= n 0)
#f
(even? (- n 1))))))
(even? x)))
;; (display "Test letrec")
;; (f 5)
;; (f 1024)
;; named-let
(define (fib n)
(let iter ((a 1)
(b 0)
(count n))
(if (= count 0)
b
(iter (+ a b) a (- count 1)))))
;; 0 1 1 2 3 5 8
(display "Test named-let")
(fib 5)
(fib 6)
;; Recursive function defination
(define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
(display "Test recursive function")
(factorial 4)
(factorial 5)
;; Lambda calculus test, will not pass
;; ((lambda (f n)
;; (if (= n 0)
;; 1
;; (* n (f f (- n 1)))))
;; '(lambda (f n)
;; (if (= n 0)
;; 1
;; (* n (f f (- n 1)))))
;; 5)
;; Omega combinator
;; Loop forever (watch CPU usage!)
;; ((lambda (x) (x x)) (lambda (x) (x x)))