altos/lambdakey-v1.0: Add back and/or macros
[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 eqv? 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-tail a b)
251   (if (zero? b)
252       a
253       (list-tail (cdr a) (- b 1))
254       )
255   )
256
257 (define (list-ref a b)
258   (car (list-tail a b))
259   )
260
261 (list-ref '(1 2 3) 2)
262     
263
264                                         ; define a set of local
265                                         ; variables one at a time and
266                                         ; then evaluate a list of
267                                         ; sexprs
268                                         ;
269                                         ; (let* (var-defines) sexprs)
270                                         ;
271                                         ; where var-defines are either
272                                         ;
273                                         ; (name value)
274                                         ;
275                                         ; or
276                                         ;
277                                         ; (name)
278                                         ;
279                                         ; e.g.
280                                         ;
281                                         ; (let* ((x 1) (y)) (set! y (+ x 1)) y)
282
283 (define let*
284   (macro (a . b)
285
286                                         ;
287                                         ; make the list of names in the let
288                                         ;
289
290          (define (_n a)
291            (cond ((not (null? a))
292                   (cons (car (car a))
293                         (_n (cdr a))))
294                  (else ())
295                  )
296            )
297
298                                         ; the set of expressions is
299                                         ; the list of set expressions
300                                         ; pre-pended to the
301                                         ; expressions to evaluate
302
303          (define (_v a b)
304            (cond ((null? a) b)           (else
305                   (cons
306                    (list set
307                          (list quote
308                                (car (car a))
309                                )
310                          (cond ((null? (cdr (car a))) ())
311                                (else (cadr (car a))))
312                          )
313                    (_v (cdr a) b)
314                    )
315                   )
316                  )
317            )
318
319                                         ; the parameters to the lambda is a list
320                                         ; of nils of the right length
321
322          (define (_z a)
323            (cond ((null? a) ())
324                  (else (cons () (_z (cdr a))))
325                  )
326            )
327                                         ; build the lambda.
328
329          (cons (cons lambda (cons (_n a) (_v a b))) (_z a))
330          )
331      )
332
333 (let* ((a 1) (y a)) (+ a y))
334
335 (define let let*)
336                                         ; recursive equality
337
338 (define (equal? a b)
339   (cond ((eq? a b) #t)
340         ((pair? a)
341          (cond ((pair? b)
342                 (cond ((equal? (car a) (car b))
343                        (equal? (cdr a) (cdr b)))
344                       )
345                 )
346                )
347          )
348         )
349   )
350
351 (equal? '(a b c) '(a b c))
352 (equal? '(a b c) '(a b b))
353
354 (define (member a b . t?)
355   (cond ((null? b)
356          #f
357          )
358         (else
359          (if (null? t?) (set! t? equal?) (set! t? (car t?)))
360          (if (t? a (car b))
361              b
362              (member a (cdr b) t?))
363          )
364         )
365   )
366
367 (member '(2) '((1) (2) (3)))
368
369 (member '(4) '((1) (2) (3)))
370
371 (define (memq a b) (member a b eq?))
372
373 (memq 2 '(1 2 3))
374
375 (memq 4 '(1 2 3))
376
377 (memq '(2) '((1) (2) (3)))
378
379 (define (_as a b t?)
380   (if (null? b)
381       #f
382     (if (t? a (caar b))
383         (car b)
384       (_as a (cdr b) t?)
385       )
386     )
387   )
388
389 (define (assq a b) (_as a b eq?))
390 (define (assoc a b) (_as a b equal?))
391
392 (assq 'a '((a 1) (b 2) (c 3)))
393 (assoc '(c) '((a 1) (b 2) ((c) 3)))
394
395 (define string (lambda a (list->string a)))
396
397 (define map
398   (lambda (a . b)
399          (define (_a b)
400            (cond ((null? b) ())
401                  (else
402                   (cons (caar b) (_a (cdr b)))
403                   )
404                  )
405            )
406          (define (_n b)
407            (cond ((null? b) ())
408                  (else
409                   (cons (cdr (car b)) (_n (cdr b)))
410                   )
411                  )
412            )
413          (define (_d b)
414            (cond ((null? (car b)) ())
415                  (else
416                   (cons (apply a (_a b)) (_d (_n b)))
417                   )
418                  )
419            )
420          (_d b)
421          )
422   )
423
424 (map cadr '((a b) (d e) (g h)))
425
426 (define (newline) (write-char #\newline))
427
428 (newline)