aboutsummaryrefslogtreecommitdiffstats
path: root/miniscm/init.scm
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2024-08-29 22:24:33 -0400
committerGravatar Peter McGoron 2024-08-29 22:24:33 -0400
commitf160ecaae1532cb61e3158756b24193fb67c895e (patch)
tree05bc9c2e02a174da3732ab9f104460c59f2b1eb6 /miniscm/init.scm
parentminiscm: add mutable string emulation and char->integer (diff)
add sets
Diffstat (limited to 'miniscm/init.scm')
-rw-r--r--miniscm/init.scm60
1 files changed, 60 insertions, 0 deletions
diff --git a/miniscm/init.scm b/miniscm/init.scm
index c3f5612..91abe72 100644
--- a/miniscm/init.scm
+++ b/miniscm/init.scm
@@ -114,3 +114,63 @@
(if (< (char->integer x) (char->integer y))
'<
'>))))))
+
+(define max
+ (lambda (curmax . rest)
+ (if (null? rest)
+ curmax
+ (let ((next-num (car rest)))
+ (apply max
+ (cons (if (> next-num curmax) next-num curmax)
+ (cdr rest)))))))
+
+(define all
+ (lambda (f l)
+ (cond
+ ((null? l) #t)
+ ((not (f (car l))) (all f (cdr l)))
+ (else #f))))
+
+(define any
+ (lambda (f l)
+ (cond
+ ((null? l) #f)
+ ((f (car l)) #t)
+ (else (any f (cdr l))))))
+
+(macro
+ cond-expand
+ (lambda (body)
+ (letrec
+ ((loop
+ (lambda (body)
+ (if (null? body)
+ #f
+ (let ((elem (car body)))
+ (cond
+ ((eqv? (car elem) 'else)
+ (cons 'begin (cdr elem)))
+ ((and (pair? elem)
+ (passes? (car elem)))
+ (cons 'begin (cdr elem)))
+ (else (loop (cdr body))))))))
+ (passes?
+ (lambda (boolean-form)
+ (cond
+ ((eqv? boolean-form 'miniscm-unslisp) #t)
+ ((eqv? boolean-form 'r3rs) #t)
+ ((symbol? boolean-form) #f)
+ ((not (pair? boolean-form)) (error "invalid boolean form"))
+ ((eqv? (car boolean-form) 'and)
+ (all passes? (cdr boolean-form)))
+ ((eqv? (car boolean-form) 'or)
+ (any passes? (cdr boolean-form)))
+ ((eqv? (car boolean-form) 'not)
+ (not (passes? (cadr boolean-form))))
+ (else (error "invalid boolean function"))))))
+ (loop (cdr body)))))
+
+(define (abs x)
+ (if (< x 0)
+ (- x)
+ x))