altos/telefireone-v1.0: Track ao_led_init API change
[fw/altos] / src / lambdakey-v1.0 / ao_lambdakey_const.scheme
1 ;
2 ; Copyright © 2016 Keith Packard <keithp@keithp.com>
3 ;
4 ; This program is free software; you can redistribute it and/or modify
5 ; it under the terms of the GNU General Public License as published by
6 ; the Free Software Foundation, either version 2 of the License, or
7 ; (at your option) any later version.
8 ;
9 ; This program is distributed in the hope that it will be useful, but
10 ; WITHOUT ANY WARRANTY; without even the implied warranty of
11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ; General Public License for more details.
13 ;
14 ; Lisp code placed in ROM
15
16                                         ; return a list containing all of the arguments
17 (def (quote list) (lambda l l))
18
19 (def (quote def!)
20      (macro (a b)
21             (list
22              def
23              (list quote a)
24              b)
25             )
26      )
27
28 (begin
29  (def! append
30    (lambda args
31           (def! a-l
32             (lambda (a b)
33               (cond ((null? a) b)
34                     (else (cons (car a) (a-l (cdr a) b)))
35                     )
36               )
37             )
38             
39           (def! a-ls
40             (lambda (l)
41               (cond ((null? l) l)
42                     ((null? (cdr l)) (car l))
43                     (else (a-l (car l) (a-ls (cdr l))))
44                     )
45               )
46             )
47           (a-ls args)
48           )
49    )
50  'append)
51
52 (append '(a) '(b))
53
54                                         ;
55                                         ; Define a variable without returning the value
56                                         ; Useful when defining functions to avoid
57                                         ; having lots of output generated.
58                                         ;
59                                         ; Also accepts the alternate
60                                         ; form for defining lambdas of
61                                         ; (define (name a y z) sexprs ...) 
62                                         ;
63
64 (begin
65  (def (quote define)
66    (macro (a . b)
67                                         ; check for alternate lambda definition form
68
69           (cond ((pair? a)
70                  (set! b
71                        (cons lambda (cons (cdr a) b)))
72                  (set! a (car a))
73                  )
74                 (else
75                  (set! b (car b))
76                  )
77                 )
78           (cons begin
79                 (cons
80                  (cons def
81                        (cons (cons quote (cons a '()))
82                              (cons b '())
83                              )
84                        )
85                  (cons
86                   (cons quote (cons a '()))
87                   '())
88                  )
89                 )
90           )
91    )
92  'define
93  )
94
95                                         ; boolean operators
96
97 (begin
98  (def! or
99    (macro a
100           (def! _or
101             (lambda (a)
102               (cond ((null? a) #f)
103                     ((null? (cdr a))
104                      (car a))
105                     (else
106                      (list
107                       cond
108                       (list
109                        (car a))
110                       (list
111                        'else
112                        (_or (cdr a))
113                        )
114                       )
115                      )
116                     )
117               )
118             )
119           (_or a)))
120  'or)
121
122                                         ; execute to resolve macros
123
124 (or #f #t)
125
126 (begin
127  (def! and
128    (macro a
129           (def! _and
130             (lambda (a)
131               (cond ((null? a) #t)
132                     ((null? (cdr a))
133                      (car a))
134                     (else
135                      (list
136                       cond
137                       (list
138                        (car a)
139                        (_and (cdr a))
140                        )
141                       )
142                      )
143                     )
144               )
145             )
146           (_and a)
147           )
148    )
149  'and)
150
151                                         ; execute to resolve macros
152
153 (and #t #f)
154
155                                         ; basic list accessors
156
157 (define (caar a) (car (car a)))
158
159 (define (cadr a) (car (cdr a)))
160
161 ; (define (cdar a) (cdr (car a)))
162
163                                         ; (if <condition> <if-true>)
164                                         ; (if <condition> <if-true> <if-false)
165
166 (define if
167   (macro (test . b)
168     (cond ((null? (cdr b))
169            (list cond (list test (car b)))
170                 )
171           (else
172            (list cond
173                  (list test (car b))
174                  (list 'else (cadr b))
175                  )
176            )
177           )
178     )
179   )
180
181 (if (> 3 2) 'yes)
182 (if (> 3 2) 'yes 'no)
183 (if (> 2 3) 'no 'yes)
184 (if (> 2 3) 'no)
185
186                                         ; simple math operators
187
188 (define zero? (macro (value) (list eq? value 0)))
189
190 (zero? 1)
191 (zero? 0)
192 (zero? "hello")
193
194 (define positive? (macro (value) (list > value 0)))
195
196 (positive? 12)
197 (positive? -12)
198
199 (define negative? (macro (value) (list < value 0)))
200
201 (negative? 12)
202 (negative? -12)
203
204 (define (abs a) (if (>= a 0) a (- a)))
205
206 (abs 12)
207 (abs -12)
208
209 (define max (lambda (a . b)
210                    (while (not (null? b))
211                      (cond ((< a (car b))
212                             (set! a (car b)))
213                            )
214                      (set! b (cdr b))
215                      )
216                    a)
217   )
218
219 (max 1 2 3)
220 (max 3 2 1)
221
222 (define min (lambda (a . b)
223                    (while (not (null? b))
224                      (cond ((> a (car b))
225                             (set! a (car b)))
226                            )
227                      (set! b (cdr b))
228                      )
229                    a)
230   )
231
232 (min 1 2 3)
233 (min 3 2 1)
234
235 (define (even? a) (zero? (% a 2)))
236
237 (even? 2)
238 (even? -2)
239 (even? 3)
240 (even? -1)
241
242 (define (odd? a) (not (even? a)))
243
244 (odd? 2)
245 (odd? -2)
246 (odd? 3)
247 (odd? -1)
248
249
250 (define (list-ref a b)
251   (car (list-tail a b))
252   )
253
254 (list-ref '(1 2 3) 2)
255     
256
257                                         ; define a set of local
258                                         ; variables one at a time and
259                                         ; then evaluate a list of
260                                         ; sexprs
261                                         ;
262                                         ; (let* (var-defines) sexprs)
263                                         ;
264                                         ; where var-defines are either
265                                         ;
266                                         ; (name value)
267                                         ;
268                                         ; or
269                                         ;
270                                         ; (name)
271                                         ;
272                                         ; e.g.
273                                         ;
274                                         ; (let* ((x 1) (y)) (set! y (+ x 1)) y)
275
276 (define letrec
277   (macro (a . b)
278
279                                         ;
280                                         ; make the list of names in the let
281                                         ;
282
283          (define (_n a)
284            (cond ((not (null? a))
285                   (cons (car (car a))
286                         (_n (cdr a))))
287                  (else ())
288                  )
289            )
290
291                                         ; the set of expressions is
292                                         ; the list of set expressions
293                                         ; pre-pended to the
294                                         ; expressions to evaluate
295
296          (define (_v a b)
297            (cond ((null? a) b)
298                  (else
299                   (cons
300                    (list set
301                          (list quote
302                                (car (car a))
303                                )
304                          (cond ((null? (cdr (car a))) ())
305                                (else (cadr (car a))))
306                          )
307                    (_v (cdr a) b)
308                    )
309                   )
310                  )
311            )
312
313                                         ; the parameters to the lambda is a list
314                                         ; of nils of the right length
315
316          (define (_z a)
317            (cond ((null? a) ())
318                  (else (cons () (_z (cdr a))))
319                  )
320            )
321                                         ; build the lambda.
322
323          (cons (cons lambda (cons (_n a) (_v a b))) (_z a))
324          )
325      )
326
327 (letrec ((a 1) (y a)) (+ a y))
328
329 (define let letrec)
330 (define let* letrec)
331                                         ; recursive equality
332
333 (define (equal? a b)
334   (cond ((eq? a b) #t)
335         ((pair? a)
336          (cond ((pair? b)
337                 (cond ((equal? (car a) (car b))
338                        (equal? (cdr a) (cdr b)))
339                       )
340                 )
341                )
342          )
343         )
344   )
345
346 (equal? '(a b c) '(a b c))
347 (equal? '(a b c) '(a b b))
348
349 (define (member a b . t?)
350   (cond ((null? b)
351          #f
352          )
353         (else
354          (if (null? t?) (set! t? equal?) (set! t? (car t?)))
355          (if (t? a (car b))
356              b
357              (member a (cdr b) t?))
358          )
359         )
360   )
361
362 (member '(2) '((1) (2) (3)))
363
364 (member '(4) '((1) (2) (3)))
365
366 (define (memq a b) (member a b eq?))
367
368 (memq 2 '(1 2 3))
369
370 (memq 4 '(1 2 3))
371
372 (memq '(2) '((1) (2) (3)))
373
374 (define (assoc a b . t?)
375   (if (null? t?)
376       (set! t? equal?)
377       (set! t? (car t?))
378       )
379   (if (null? b)
380       #f
381     (if (t? a (caar b))
382         (car b)
383       (assoc a (cdr b) t?)
384       )
385     )
386   )
387
388 (define (assq a b) (assoc a b eq?))
389
390 (assq 'a '((a 1) (b 2) (c 3)))
391 (assoc '(c) '((a 1) (b 2) ((c) 3)))
392
393 (define string (lambda a (list->string a)))
394
395 (define map
396   (lambda (a . b)
397          (define (_a b)
398            (cond ((null? b) ())
399                  (else
400                   (cons (caar b) (_a (cdr b)))
401                   )
402                  )
403            )
404          (define (_n b)
405            (cond ((null? b) ())
406                  (else
407                   (cons (cdr (car b)) (_n (cdr b)))
408                   )
409                  )
410            )
411          (define (_d b)
412            (cond ((null? (car b)) ())
413                  (else
414                   (cons (apply a (_a b)) (_d (_n b)))
415                   )
416                  )
417            )
418          (_d b)
419          )
420   )
421
422 (map cadr '((a b) (d e) (g h)))
423
424 (define (newline) (write-char #\newline))
425
426 (newline)