Creating the set of all possible subsets, the powerset of a set with Scheme.
Creating the set of all possible subsets, the powerset of a set with Scheme.
Date Created:Tuesday September 23rd, 2008 05:08 PM
Date Modified:Tuesday April 13th, 2010 09:49 PM
;; RETURNS POWERSET OF SET s (define (powerset s) (if (null? s) (list nil) (let ((rest (powerset (cdr s)))) (append rest (map (lambda (x) (cons (car s) x)) rest))))) ;; ITER RETURNS SUBSETS OF SIZE n (define (iter s n new) (if (null? s) new (if (= (length (car s)) n) (iter (cdr s) n (cons (car s) new)) (iter (cdr s) n new))))
Downloads:
Download: powerset.scm 407 B
Please login or Click Here to register for downloads
Powersets by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
