altos/lambdakey-v1.0: Get this building again
[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 c) '(d e f) '(g h i))
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 ((list? 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                                         ; basic list accessors
96
97 (define (caar l) (car (car l)))
98
99 (define (cadr l) (car (cdr l)))
100
101 (define (cdar l) (cdr (car l)))
102
103 (define (caddr l) (car (cdr (cdr l))))
104
105                                         ; (if <condition> <if-true>)
106                                         ; (if <condition> <if-true> <if-false)
107
108 (define if
109   (macro (test . args)
110     (cond ((null? (cdr args))
111            (list cond (list test (car args)))
112                 )
113           (else
114            (list cond
115                  (list test (car args))
116                  (list 'else (cadr args))
117                  )
118            )
119           )
120     )
121   )
122
123 (if (> 3 2) 'yes)
124 (if (> 3 2) 'yes 'no)
125 (if (> 2 3) 'no 'yes)
126 (if (> 2 3) 'no)
127
128                                         ; simple math operators
129
130 (define zero? (macro (value) (list eqv? value 0)))
131
132 (zero? 1)
133 (zero? 0)
134 (zero? "hello")
135
136 (define positive? (macro (value) (list > value 0)))
137
138 (positive? 12)
139 (positive? -12)
140
141 (define negative? (macro (value) (list < value 0)))
142
143 (negative? 12)
144 (negative? -12)
145
146 (define (abs a) (if (>= a 0) a (- a)))
147
148 (abs 12)
149 (abs -12)
150
151 (define max (lambda (a . b)
152                    (while (not (null? b))
153                      (cond ((< a (car b))
154                             (set! a (car b)))
155                            )
156                      (set! b (cdr b))
157                      )
158                    a)
159   )
160
161 (max 1 2 3)
162 (max 3 2 1)
163
164 (define min (lambda (a . b)
165                    (while (not (null? b))
166                      (cond ((> a (car b))
167                             (set! a (car b)))
168                            )
169                      (set! b (cdr b))
170                      )
171                    a)
172   )
173
174 (min 1 2 3)
175 (min 3 2 1)
176
177 (define (even? a) (zero? (% a 2)))
178
179 (even? 2)
180 (even? -2)
181 (even? 3)
182 (even? -1)
183
184 (define (odd? a) (not (even? a)))
185
186 (odd? 2)
187 (odd? -2)
188 (odd? 3)
189 (odd? -1)
190
191
192 (define (list-tail a b)
193   (if (zero? b)
194       a
195     (list-tail (cdr a (- b 1)))
196     )
197   )
198
199 (define (list-ref a b)
200   (car (list-tail a b))
201   )
202
203 (define (list-tail a b)
204   (if (zero? b)
205       a
206     (list-tail (cdr a) (- b 1))))
207
208 (list-tail '(1 2 3) 2)
209
210 (define (list-ref a b) (car (list-tail a b)))
211
212 (list-ref '(1 2 3) 2)
213     
214
215                                         ; define a set of local
216                                         ; variables one at a time and
217                                         ; then evaluate a list of
218                                         ; sexprs
219                                         ;
220                                         ; (let* (var-defines) sexprs)
221                                         ;
222                                         ; where var-defines are either
223                                         ;
224                                         ; (name value)
225                                         ;
226                                         ; or
227                                         ;
228                                         ; (name)
229                                         ;
230                                         ; e.g.
231                                         ;
232                                         ; (let* ((x 1) (y)) (set! y (+ x 1)) y)
233
234 (define let*
235   (macro (a . b)
236
237                                         ;
238                                         ; make the list of names in the let
239                                         ;
240
241          (define (_n a)
242            (cond ((not (null? a))
243                   (cons (car (car a))
244                         (_n (cdr a))))
245                  (else ())
246                  )
247            )
248
249                                         ; the set of expressions is
250                                         ; the list of set expressions
251                                         ; pre-pended to the
252                                         ; expressions to evaluate
253
254          (define (_v a b)
255            (cond ((null? a) b)           (else
256                   (cons
257                    (list set
258                          (list quote
259                                (car (car a))
260                                )
261                          (cond ((null? (cdr (car a))) ())
262                                (else (cadr (car a))))
263                          )
264                    (_v (cdr a) b)
265                    )
266                   )
267                  )
268            )
269
270                                         ; the parameters to the lambda is a list
271                                         ; of nils of the right length
272
273          (define (_z a)
274            (cond ((null? a) ())
275                  (else (cons () (_z (cdr a))))
276                  )
277            )
278                                         ; build the lambda.
279
280          (cons (cons lambda (cons (_n a) (_v a b))) (_z a))
281          )
282      )
283
284 (let* ((a 1) (y a)) (+ a y))
285
286 (define let let*)
287                                         ; recursive equality
288
289 (define (equal? a b)
290   (cond ((eq? a b) #t)
291         ((pair? a)
292          (cond ((pair? b)
293                 (cond ((equal? (car a) (car b))
294                        (equal? (cdr a) (cdr b)))
295                       )
296                 )
297                )
298          )
299         )
300   )
301
302 (equal? '(a b c) '(a b c))
303 (equal? '(a b c) '(a b b))
304
305 (define member (lambda (obj a . test?)
306                       (cond ((null? a)
307                              #f
308                              )
309                             (else
310                              (if (null? test?) (set! test? equal?) (set! test? (car test?)))
311                              (if (test? obj (car a))
312                                  a
313                                (member obj (cdr a) test?))
314                              )
315                             )
316                       )
317   )
318
319 (member '(2) '((1) (2) (3)))
320
321 (member '(4) '((1) (2) (3)))
322
323 (define (memq obj a) (member obj a eq?))
324
325 (memq 2 '(1 2 3))
326
327 (memq 4 '(1 2 3))
328
329 (memq '(2) '((1) (2) (3)))
330
331 (define (_assoc a b t?)
332   (if (null? b)
333       #f
334     (if (t? a (caar b))
335         (car b)
336       (_assoc a (cdr b) t?)
337       )
338     )
339   )
340
341 (define (assq a b) (_assoc a b eq?))
342 (define (assoc a b) (_assoc a b equal?))
343
344 (assq 'a '((a 1) (b 2) (c 3)))
345 (assoc '(c) '((a 1) (b 2) ((c) 3)))
346
347 (define string (lambda a (list->string a)))
348
349 (display "apply\n")
350 (apply cons '(a b))
351
352 (define map
353   (lambda (a . b)
354          (define (args b)
355            (cond ((null? b) ())
356                  (else
357                   (cons (caar b) (args (cdr b)))
358                   )
359                  )
360            )
361          (define (next b)
362            (cond ((null? b) ())
363                  (else
364                   (cons (cdr (car b)) (next (cdr b)))
365                   )
366                  )
367            )
368          (define (domap b)
369            (cond ((null? (car b)) ())
370                  (else
371                   (cons (apply a (args b)) (domap (next b)))
372                   )
373                  )
374            )
375          (domap b)
376          )
377   )
378
379 (map cadr '((a b) (d e) (g h)))
380
381 (define for-each (lambda (a . b)
382                         (apply map a b)
383                         #t))
384
385 (for-each display '("hello" " " "world" "\n"))
386
387 (define (newline) (write-char #\newline))
388
389 (newline)