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