Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Subsets of a list
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
George Caswell  
View profile  
 More options Jan 11 2002, 4:00 am
Newsgroups: comp.lang.scheme
From: George Caswell <tetsu...@maine.rr.com>
Date: Fri, 11 Jan 2002 08:57:39 GMT
Local: Fri, Jan 11 2002 3:57 am
Subject: Re: Subsets of a list
On 10 Jan 2002, Max Hailperin wrote:

> George Caswell <tetsu...@maine.rr.com> wrote in message <news:Pine.LNX.3.96.1020110005414.7936B-100000@adamant.homeworlds.net>...
> ...
> [tree-recursive procedure to find subsets of size n from a list, l]
> >    The basic problem here is that certain subsets are being calculated more
> > than once:  ...[comparison with tree-recursive fib]...

> Actually, there is no big efficiency gain to be had in this case,
> because the list that is the result is of length C(|l|, n), so it is
> inevitable that any procedure for consing up that list will take time
> of that order -- all that you can do is improve the constant factor.
> The big win, through dynamic programming or memoization, is only
> possible if the problem is changed to something like counting how many
> subsets there are.

   Hrm, I guess you're right, though I wouldn't have thought so.  My brain's
having a little difficulty with why that's the case.

   Anyway, my implementation, "subs" uses up a lot of heap space compared to,
say, "combos" posted by John Stone, and incurs more garbage collection, but
still comes out faster:

(define (expand xs sets)
    (if (or (null? xs) (null? sets))
        ()
        (cons
            (apply append (map (lambda (set) (map (lambda (s) (cons (car xs)
s)) set)) (cdr sets)))
            (expand (cdr xs) (cdr sets)))
))

(define (subs xs n)
    (cond ((eq? 0 n) ())
        (else
            (do ((i 1 (+ i 1))
                 (acc (map (lambda (x) (list (list x))) xs) (expand xs acc)))
                ((= i n) (apply append acc))))))

   Sorry if I spread a bunch of misinformation with that tree-recursion
comparison: I'm still not sure why I'm wrong, but I had the same problem with
the Monty Hall thing.

---GEC
Projects page: http://home.maine.rr.com/tetsujin/
(M-x depeche-mode)
"Must...  Finish...  Zaku..."


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.