From 44b26569a63b769124c7e316cddad90053726601 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sat, 16 Jul 2016 20:02:14 +0200 Subject: [PATCH] implement variable-argument and, or. --- scm.mes | 23 +++++++++++++++++++++-- test.mes | 24 ++++++++++++++++-------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/scm.mes b/scm.mes index 80e69098..ff590564 100755 --- a/scm.mes +++ b/scm.mes @@ -46,12 +46,24 @@ (#t (memq x (cdr lst))))) (define memv memq) -(define-macro (or x y) +(define-macro (or2 x y) `(cond (,x ,x) (#t ,y))) -(define-macro (and x y) +(define-macro (and2 x y) `(cond (,x ,y) (#t #f))) +(define-macro (or . x) + (cond + ((null? x) #f) + ((null? (cdr x)) (car x)) + (#t `(cond (,(car x)) + (#t (or ,@(cdr x))))))) + +(define-macro (and . x) + (cond ((null? x) #t) + ((null? (cdr x)) (car x)) + (#t `(cond (,(car x) (and ,@(cdr x))))))) + (define (split-params bindings params) (cond ((null? bindings) params) (#t (split-params (cdr bindings) @@ -121,3 +133,10 @@ ,@(letrec-setters bindings '()) ,@body)) +;; TODO +;; (define gensym +;; (let ((counter 0)) +;; (lambda (. rest) +;; (let ((val (number->string counter))) +;; (set! counter (+ counter 1)) +;; (string->symbol (string-append "g" val)))))) diff --git a/test.mes b/test.mes index 6bd118c7..6e63ae51 100644 --- a/test.mes +++ b/test.mes @@ -42,18 +42,26 @@ (newline) -(display 'and-0-1:) -(display (and 0 1)) +(display "(and): ") +(display (and)) (newline) -(display 'and-#f-2:) -(display (and #f 2)) +(display "(and 1): ") +(display (and 1)) +(newline) +(display "(and 1 (= 0 1)): ") +(display (and 1 (= 0 1))) (newline) -(display 'or-0-1:) -(display (or 0 1)) +(display "(or): ") +(display (or)) (newline) -(display 'or-#f-2:) -(display (or #f 2)) +(display "(or 1): ") +(display (or 1)) +(newline) +(display "(or #f (= 0 1) 3): ") +(display (or #f (= 0 1) 3)) +(newline) +(display (or (= 0 1) #f (and (display "YEAH") (newline) 'woet))) (newline) (let ((p 5)