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
 
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


 
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.