implement variable-argument and, or.

This commit is contained in:
Jan Nieuwenhuizen 2016-07-16 20:02:14 +02:00
parent 47fd339305
commit 44b26569a6
2 changed files with 37 additions and 10 deletions

23
scm.mes
View file

@ -46,12 +46,24 @@
(#t (memq x (cdr lst))))) (#t (memq x (cdr lst)))))
(define memv memq) (define memv memq)
(define-macro (or x y) (define-macro (or2 x y)
`(cond (,x ,x) (#t ,y))) `(cond (,x ,x) (#t ,y)))
(define-macro (and x y) (define-macro (and2 x y)
`(cond (,x ,y) (#t #f))) `(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) (define (split-params bindings params)
(cond ((null? bindings) params) (cond ((null? bindings) params)
(#t (split-params (cdr bindings) (#t (split-params (cdr bindings)
@ -121,3 +133,10 @@
,@(letrec-setters bindings '()) ,@(letrec-setters bindings '())
,@body)) ,@body))
;; TODO
;; (define gensym
;; (let ((counter 0))
;; (lambda (. rest)
;; (let ((val (number->string counter)))
;; (set! counter (+ counter 1))
;; (string->symbol (string-append "g" val))))))

View file

@ -42,18 +42,26 @@
(newline) (newline)
(display 'and-0-1:) (display "(and): ")
(display (and 0 1)) (display (and))
(newline) (newline)
(display 'and-#f-2:) (display "(and 1): ")
(display (and #f 2)) (display (and 1))
(newline)
(display "(and 1 (= 0 1)): ")
(display (and 1 (= 0 1)))
(newline) (newline)
(display 'or-0-1:) (display "(or): ")
(display (or 0 1)) (display (or))
(newline) (newline)
(display 'or-#f-2:) (display "(or 1): ")
(display (or #f 2)) (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) (newline)
(let ((p 5) (let ((p 5)