forked from shixiongfei/ape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ape
254 lines (179 loc) · 4.47 KB
/
test.ape
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
;; test.ape
;;
;; Copyright (c) 2021-2024 Xiongfei Shi
;;
;; Author: Xiongfei Shi <xiongfei.shi(a)icloud.com>
;; License: Apache-2.0
;;
;; https://github.com/shixiongfei/ape
;;
(print (length '(1 2 3 4 5)))
(print (string-length "Hello, World!"))
(print (vector-length #(1 2 3 4 5)))
(print (list-ref 1 '(1 2 3 4 5)))
(print (string-ref 1 "Hello, World!"))
(print (vector-ref 1 #(1 2 3 4 5)))
(print (reverse '(1 2 3 4 5)))
(print (string-reverse "HelloWorld"))
(print (append '(1 2 3)))
(print (append '(1 2 3) '(4 5 6) '(7 8 9)))
(print (append '(1 2 3) 4))
(print (list->vector '(1 2 3 4 5)))
(print (vector->list #(1 2 3 4 5)))
(print (string->list "HelloWorld"))
(print (string->vector "HelloWorld"))
(print (car '(1 2 3 4 5)))
(print (cdr '(1 2 3 4 5)))
(print (do (cons 1 2)))
(print (+ 1 2 3 4 5))
(print (- 10 4 3 2))
(print (* 2 3 4 5 6))
(print (/ 10 4 3 2))
(print (rem 10 3))
(print (type (/ 10 3)))
(print (type (/ 10 2)))
(print (type (string 1234)))
(print (type (string 'xyz)))
(print (type (number "-12.34")))
(print (type (symbol (string-append "hello" "-" "world"))))
(print "gensym" (gensym))
(print "gensym" (gensym))
(def x 100)
(print "x =" x)
(set! x 150)
(print "x =" x)
(print (> 1 2))
(print (>= 1 2))
(print (> 2 1))
(print (>= 2 2))
(print (< 1 2))
(print (<= 1 2))
(print (< 2 1))
(print (<= 2 2))
(print (and 1 2))
(print (and 1 nil 2))
(print (or nil 2))
(print (or 1 2))
(print (not nil))
(def c (cons 1 2))
(print "c =" c)
(set-car! c 4)
(set-cdr! c 5)
(print "c =" c)
(defn say-hello ((name "Ape"))
(string-append "Hello " name "!"))
(print (say-hello))
(print (say-hello "World"))
(defn sum (a b)
(+ a b))
(print "sum 2 3 =" (sum 2 3))
(defn fib (n)
(if (<= 2 n)
(+ (fib (- n 1)) (fib (- n 2)))
n))
(print "fib 28 =" (fib 28))
(defn counter (val step)
(def count val)
(fn ()
(set! count (+ count step))))
(def count (counter 0 1))
(print "Count" (count))
(print "Count" (count))
(print "Count" (count))
(print "Count" (count))
(print "Count" (count))
(def items (list "cat" "dog" "fox"))
(push "owl" items)
(push "cow" items)
(push "wolf" items)
(print (pop items))
(print items)
(print (expand '(push "bird" items)))
(defn accum (n)
(defn iter (acc step)
(cond
(> step n) acc
(iter (+ acc step) (+ step 1))))
(iter 0 0))
(print "accum 100 =" (accum 100))
(print (eval '(+ 1 2 3 4 5)))
(when true
(set! x 200)
(print "when x =" x))
(unless nil
(set! x 250)
(print "unless x =" x))
(set! x 10)
(while (> x 0)
(print "x =" x)
(set! x (- x 1)))
(print "global x =" x)
(print "let x + y =" (let (x (set! x (+ x 1))
y 8)
(+ x y)))
(print "global x =" x)
(for x items
(print ">" x))
(print items)
(for x (list 1 2 3 4 5)
(print x))
(for x '(a b c d e)
(print x))
(def pl '(x 100 y 200 z 300))
(print "pl:" pl)
(print "pl x:" (get pl 'x))
(print "pl y:" (get pl 'y))
(print "pl z:" (get pl 'z))
(print "pl w:" (get pl 'w 400))
(def al (acons 'x 10 (acons 'y 20 (acons 'z 30 nil))))
(print "al:" al)
(print "al x:" (assoc 'x al))
(print "al y:" (assoc 'y al))
(print "al z:" (assoc 'z al))
(print "map 1+ =" (map (fn (x) (+ x 1))
'(1 2 3 4 5)))
(print "filter >5 =" (filter (fn (x) (> x 5))
'(1 2 3 4 5 6 7 8 9)))
(print "recude + =" (reduce + 0 '(1 2 3 4 5)))
(print "apply + =" (apply + '(1 2 3 4 5)))
(print "nesting unquote =" (let (x 100
y 'x
z 'y)
`(,,,z)))
(print "nesting unquote-splicing =" `(,@,@'('(1 2 3)))
`(,@,''(4 5 6)))
(defn y-combinator (f)
((fn (u)
(u u))
(fn (x)
(f (fn args
(apply (x x) args))))))
(def y-fib
(y-combinator
(fn (fibonacci)
(fn (n)
(if (<= 2 n)
(+ (fibonacci (- n 1)) (fibonacci (- n 2)))
n)))))
(print "y-fib 10 =" (y-fib 10))
(def y-factorial
(y-combinator
(fn (factorial)
(fn (n)
(if (= n 0)
1
(* n (factorial (- n 1))))))))
(print "y-factorial 10 =" (y-factorial 10))
(defn heart ()
(def rows (map (fn (row) (/ row 10))
(range 15 -15 -1)))
(def cols (map (fn (col) (/ col 10))
(range -15 15 0.5)))
(for y rows
(for x cols
(let (a (- (+ (* x x) (* y y)) 1.0))
(if (<= (- (* a a a) (* x x y y y)) 0.0)
(write "*")
(write "."))))
(print)))
(heart)