Re: Homework Help! Subsets

Liste des GroupesRevenir à cl scheme 
Sujet : Re: Homework Help! Subsets
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 31. Jul 2025, 06:45:46
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <106evu6$3jgkr$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:

John Gilson wrote:
 
(defun power-set (set)
    (let ((power-set (list ()))) ; power set always contains empty set
       (labels
          ((insert (subset)
              (loop for elt in set
                        until (eql elt (first subset))
                        collect (cons elt subset)))
           (power-set-length-n (length-m-sets)
               (mapcan #'(lambda (length-m-set)
                                   (insert length-m-set))
                             length-m-sets))
           (power-set-aux (length-m-sets)
               ;; n = m + 1
               (unless (null length-m-sets)
                  (let ((length-n-sets (power-set-length-n length-m-sets)))
                     (setf power-set (append length-n-sets power-set))
                     (power-set-aux length-n-sets)))))
          (power-set-aux power-set))
       power-set))
 
(power-set '(1 2 3))
((1 2 3) (1 2) (1 3) (2 3) (1) (2) (3) NIL)
 
 
Scheme
 
;; Adapted from a CL version.
(define (powerset List)
  (if (null? List)
    '(())
    (let* ((x (car List))
           (p (powerset (cdr List))))
      (append p
        (map (lambda(xs) (cons x xs)) p)))))
 
(powerset '(a b c))
  ===>
(() (c) (b) (b c) (a) (a c) (a b) (a b c))
 
 
(powerset '(a b c d))
  ===>
(() (d) (c) (c d) (b) (b d) (b c) (b c d) (a) (a d) (a c) (a c d)
 (a b) (a b d) (a b c) (a b c d))

Gauche Scheme

(use util.match)

(define powerset
  (match-lambda
    [() '(())]
    [(h . t) (let1 p (powerset t) `(,@p ,@(map. cons h p)))]))
   
Given:

(define (map. func obj seq)
  (map (lambda(x) (func obj x)) seq))


Another way:

(use util.combinations)

(define (powerset L)
  (append-map (^n (combinations L n)) (iota (+ 1 (length L)))))



Date Sujet#  Auteur
7 Jul 25 * Re: Homework Help! Subsets3B. Pym
31 Jul 25 `* Re: Homework Help! Subsets2B. Pym
31 Jul 25  `- Re: Homework Help! Subsets1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal