Jump to content

Set consolidation: Difference between revisions

Added Racket
(Added Racket)
Line 964:
else: r.append(x)
return r</lang>
 
=={{header|Racket}}==
<lang racket>
#lang racket
(define (consolidate ss)
(define (comb s cs)
(cond [(set-empty? s) cs]
[(empty? cs) (list s)]
[(set-empty? (set-intersect s (first cs)))
(cons (first cs) (comb s (rest cs)))]
[(consolidate (cons (set-union s (first cs)) (rest cs)))]))
(foldl comb '() ss))
 
(consolidate (list (set 'a 'b) (set 'c 'd)))
(consolidate (list (set 'a 'b) (set 'b 'c)))
(consolidate (list (set 'a 'b) (set 'c 'd) (set 'd 'b)))
(consolidate (list (set 'h 'i 'k) (set 'a 'b) (set 'c 'd) (set 'd 'b) (set 'f 'g 'h)))
</lang>
Output:
<lang racket>
(list (set 'b 'a) (set 'd 'c))
(list (set 'a 'b 'c))
(list (set 'a 'b 'd 'c))
(list (set 'g 'h 'k 'i 'f) (set 'a 'b 'd 'c))
</lang>
 
 
=={{header|REXX}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.