Fixed AND and OR implementations
authorTim Vaughan <tgvaughan@gmail.com>
Sun, 6 Nov 2016 08:41:33 +0000 (21:41 +1300)
committerTim Vaughan <tgvaughan@gmail.com>
Sun, 6 Nov 2016 08:41:33 +0000 (21:41 +1300)
scheme-library.scm

index aad91f4..f95ed65 100644 (file)
 ; and
 
 (define (expand-and-expressions expressions)
-  (if (null? expressions)
-    #t
-    (let ((first (car expressions))
-          (rest (cdr expressions)))
+  (let ((first (car expressions))
+        (rest (cdr expressions)))
+    (if (null? rest)
+      first
       `(if ,first
          ,(expand-and-expressions rest)
          #f))))
 
 (define-macro (and . expressions)
-              (expand-and-expressions expressions))
+              (if (null? expressions)
+                #t
+                (expand-and-expressions expressions)))
 
 ; or
 
   (if (null? expressions)
     #f
     (let ((first (car expressions))
-          (rest (cdr expressions)))
-      `(if ,first
-         #t
-         ,(expand-or-expressions rest)))))
+          (rest (cdr expressions))
+          (val (gensym)))
+      `(let ((,val ,first))
+         (if ,val
+            ,val
+            ,(expand-or-expressions rest))))))
 
 (define-macro (or . expressions)
               (expand-or-expressions expressions))