-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.snap
More file actions
106 lines (92 loc) · 1.89 KB
/
test.snap
File metadata and controls
106 lines (92 loc) · 1.89 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
; vi: filetype=scheme
; args
(println "args:" args)
; tail recursion
(println "tail recursion:")
(def countdown (fn [x]
(if (> x 0)
(begin (println x) (recur (- x 1)))
0)
))
(print
(countdown 1000))
; let
(println "let:")
(println (let [[a 1]
[b 2]]
(+ a b)))
; closure
(println "closure:")
(def inc (fn [x]
(fn []
(begin
(set! x (+ x 1))
x)
)
))
(def inc1 (inc 0))
(println (inc1))
(println (inc1))
(println (inc1))
(println (inc1))
(def counter (fn [n]
(let [[s 0]]
(fn []
(let [[c s]]
(if (< s n)
(begin (set! s (+ s 1)) c)
nil
)
)
)
)
))
(def a (counter 10))
((fn []
(let [[r (a)]]
(if (nil? r) nil (begin (println r) (recur)))
)
))
; recursion
(println "recursion:")
(def fact (fn [n]
(if (= n 1) 1
(* n (fact (- n 1)))
)
))
(println (fact 20))
; loop
(println "loop:")
(def i 0)
(println
((fn [n]
(println n) (set! i (+ i 10)) (if (< n 9) (recur (+ n 1)) n)
) 0)
)
(println i)
; fibonacci
(println "fibonacci:")
(def fib
(fn [c p n]
(println c)
(if (> n 0)
(recur p (+ c p) (- n 1))
nil
)
)
)
(fib 1 1 1000)
; error
(def recerr (fn [n]
(println n)
(if (= n 1)
(begin (println "Going to throw") (raise :err "Inside a recursive func"))
(recerr (- n 1)))
)
)
;; varargs
;(print "varargs:")
;((fn (a b c...)
; (print a b c)) 1 2 3 4)
;
;