2 ; Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
14 ; Lisp code placed in ROM
16 ; return a list containing all of the arguments
17 (def (quote list) (lambda l l))
34 (else (cons (car a) (a-l (cdr a) b)))
42 ((null? (cdr l)) (car l))
43 (else (a-l (car l) (a-ls (cdr l))))
55 ; Define a variable without returning the value
56 ; Useful when defining functions to avoid
57 ; having lots of output generated.
59 ; Also accepts the alternate
60 ; form for defining lambdas of
61 ; (define (name a y z) sexprs ...)
67 ; check for alternate lambda definition form
71 (cons lambda (cons (cdr a) b)))
81 (cons (cons quote (cons a '()))
86 (cons quote (cons a '()))
122 ; execute to resolve macros
151 ; execute to resolve macros
155 ; basic list accessors
157 (define (caar a) (car (car a)))
159 (define (cadr a) (car (cdr a)))
161 ; (define (cdar a) (cdr (car a)))
163 ; (if <condition> <if-true>)
164 ; (if <condition> <if-true> <if-false)
168 (cond ((null? (cdr b))
169 (list cond (list test (car b)))
174 (list 'else (cadr b))
182 (if (> 3 2) 'yes 'no)
183 (if (> 2 3) 'no 'yes)
186 ; simple math operators
188 (define zero? (macro (value) (list eqv? value 0)))
194 (define positive? (macro (value) (list > value 0)))
199 (define negative? (macro (value) (list < value 0)))
204 (define (abs a) (if (>= a 0) a (- a)))
209 (define max (lambda (a . b)
210 (while (not (null? b))
222 (define min (lambda (a . b)
223 (while (not (null? b))
235 (define (even? a) (zero? (% a 2)))
242 (define (odd? a) (not (even? a)))
250 (define (list-tail a b)
253 (list-tail (cdr a) (- b 1))
257 (define (list-ref a b)
258 (car (list-tail a b))
261 (list-ref '(1 2 3) 2)
264 ; define a set of local
265 ; variables one at a time and
266 ; then evaluate a list of
269 ; (let* (var-defines) sexprs)
271 ; where var-defines are either
281 ; (let* ((x 1) (y)) (set! y (+ x 1)) y)
287 ; make the list of names in the let
291 (cond ((not (null? a))
298 ; the set of expressions is
299 ; the list of set expressions
301 ; expressions to evaluate
304 (cond ((null? a) b) (else
310 (cond ((null? (cdr (car a))) ())
311 (else (cadr (car a))))
319 ; the parameters to the lambda is a list
320 ; of nils of the right length
324 (else (cons () (_z (cdr a))))
329 (cons (cons lambda (cons (_n a) (_v a b))) (_z a))
333 (let* ((a 1) (y a)) (+ a y))
342 (cond ((equal? (car a) (car b))
343 (equal? (cdr a) (cdr b)))
351 (equal? '(a b c) '(a b c))
352 (equal? '(a b c) '(a b b))
354 (define (member a b . t?)
359 (if (null? t?) (set! t? equal?) (set! t? (car t?)))
362 (member a (cdr b) t?))
367 (member '(2) '((1) (2) (3)))
369 (member '(4) '((1) (2) (3)))
371 (define (memq a b) (member a b eq?))
377 (memq '(2) '((1) (2) (3)))
389 (define (assq a b) (_as a b eq?))
390 (define (assoc a b) (_as a b equal?))
392 (assq 'a '((a 1) (b 2) (c 3)))
393 (assoc '(c) '((a 1) (b 2) ((c) 3)))
395 (define string (lambda a (list->string a)))
402 (cons (caar b) (_a (cdr b)))
409 (cons (cdr (car b)) (_n (cdr b)))
414 (cond ((null? (car b)) ())
416 (cons (apply a (_a b)) (_d (_n b)))
424 (map cadr '((a b) (d e) (g h)))
426 (define (newline) (write-char #\newline))