(lset-union): Rewrite to accumulate result by consing in
authorKevin Ryde <user42@zip.com.au>
Fri, 1 Apr 2005 23:47:42 +0000 (23:47 +0000)
committerKevin Ryde <user42@zip.com.au>
Fri, 1 Apr 2005 23:47:42 +0000 (23:47 +0000)
the order specified by the SRFI.

srfi/srfi-1.scm

index 4ddf12d..6137618 100644 (file)
        (lp (cdr l) (cons (car l) acc))))))
 
 (define (lset-union = . rest)
-  (let lp0 ((l rest) (acc '()))
-    (if (null? l)
-      (reverse! acc)
-      (let lp1 ((ll (car l)) (acc acc))
-       (if (null? ll)
-         (lp0 (cdr l) acc)
-         (if (member (car ll) acc (lambda (x y) (= y x)))
-           (lp1 (cdr ll) acc)
-           (lp1 (cdr ll) (cons (car ll) acc))))))))
+  (let ((acc '()))
+    (for-each (lambda (lst)
+               (if (null? acc)
+                   (set! acc lst)
+                   (for-each (lambda (elem)
+                               (if (not (member elem acc
+                                                (lambda (x y) (= y x))))
+                                   (set! acc (cons elem acc))))
+                             lst)))
+             rest)
+    acc))
 
 (define (lset-intersection = list1 . rest)
   (let lp ((l list1) (acc '()))