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 eq? 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-ref a b)
251 (car (list-tail a b))
254 (list-ref '(1 2 3) 2)
257 ; define a set of local
258 ; variables one at a time and
259 ; then evaluate a list of
262 ; (let* (var-defines) sexprs)
264 ; where var-defines are either
274 ; (let* ((x 1) (y)) (set! y (+ x 1)) y)
280 ; make the list of names in the let
284 (cond ((not (null? a))
291 ; the set of expressions is
292 ; the list of set expressions
294 ; expressions to evaluate
304 (cond ((null? (cdr (car a))) ())
305 (else (cadr (car a))))
313 ; the parameters to the lambda is a list
314 ; of nils of the right length
318 (else (cons () (_z (cdr a))))
323 (cons (cons lambda (cons (_n a) (_v a b))) (_z a))
327 (letrec ((a 1) (y a)) (+ a y))
337 (cond ((equal? (car a) (car b))
338 (equal? (cdr a) (cdr b)))
346 (equal? '(a b c) '(a b c))
347 (equal? '(a b c) '(a b b))
349 (define (member a b . t?)
354 (if (null? t?) (set! t? equal?) (set! t? (car t?)))
357 (member a (cdr b) t?))
362 (member '(2) '((1) (2) (3)))
364 (member '(4) '((1) (2) (3)))
366 (define (memq a b) (member a b eq?))
372 (memq '(2) '((1) (2) (3)))
374 (define (assoc a b . t?)
388 (define (assq a b) (assoc a b eq?))
390 (assq 'a '((a 1) (b 2) (c 3)))
391 (assoc '(c) '((a 1) (b 2) ((c) 3)))
393 (define string (lambda a (list->string a)))
400 (cons (caar b) (_a (cdr b)))
407 (cons (cdr (car b)) (_n (cdr b)))
412 (cond ((null? (car b)) ())
414 (cons (apply a (_a b)) (_d (_n b)))
422 (map cadr '((a b) (d e) (g h)))
424 (define (newline) (write-char #\newline))