Google Groups Home
Help | Sign in
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
Alamo Petrofsky  
View profile
 More options Jan 11 2002, 3:09 pm
Newsgroups: comp.lang.scheme
From: Alamo Petrofsky <al...@petrofsky.org>
Date: 11 Jan 2002 12:09:17 -0800
Local: Fri, Jan 11 2002 3:09 pm
Subject: Re: Subsets of a list
John David Stone <st...@cs.grinnell.edu> writes:

>         Here's a version that appears to be faster.  It uses an accumulator
> instead of a non-local variable and uses recursion over the list l2 rather
> than over positions in that list, thus avoiding all of the calls to
> LIST-REF and LIST-TAIL.

But it seems you still make a lot of calls to LENGTH, which similarly
do needless list traversals.  Here's how I would write it:

  ;; Returns all n-length subsets of the list l.
  (define (subsets l n)
    ;; ss prepends each n-length subset of l to little-acc, and returns
    ;; a list of the resulting sets, prepended to big-acc.  len must be
    ;; the length of l.
    (let ss ((l l) (len (length l)) (n n) (little-acc '()) (big-acc '()))
      (cond ((zero? n) (cons little-acc big-acc))
            ((< len n) big-acc)
            (else (ss (cdr l) (- len 1) (- n 1) (cons (car l) little-acc)
                      (ss (cdr l) (- len 1) n little-acc big-acc))))))

Is that faster or slower on your system?

-al


    Reply to author    Forward  
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.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google