Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!yeshua.marcam.com!zip.eecs.umich.edu!caen!msuinfo!harbinger.cc.monash.edu.au!bunyip.cc.uq.oz.au!marlin.jcu.edu.au!coral.cs.jcu.edu.au!geoff
From: geoff@coral.cs.jcu.edu.au (Geoff Sutcliffe)
Subject: Subset code
Message-ID: <geoff.784196084@coral.cs.jcu.edu.au>
Summary: Order of subsets
Keywords: Subsets
Sender: news@marlin.jcu.edu.au (USENET News System)
Organization: James Cook University
Date:  7 Nov 94 08:14:44 GMT
Lines: 48

Hi,

I have an application in which I need to create, on backtracking, all 
subsets of a given set in decreasing order of size. Example
?- subset([q,w,e]).
must, by backtracking, return [q,w,e] [q,w] [q,e] [w,e] [q] [w] [e] [] 
_in that order_.

My solution seems inelegant :
%----------------------------------------------------------------------
subset(Set):-
    length(Set,SetLength),
    integer_in_range(0,SetLength,NumberToRemove),
    remove_N(Set,NumberToRemove,Subset),
    write(Subset),nl,
    fail.
%----------------------------------------------------------------------
%----Provide a range of integers through backtracking
integer_in_range(Low,High,Low):-
    Low =< High.

integer_in_range(Low,High,Integer):-
    Low < High,
    NewLow is Low + 1,
    integer_in_range(NewLow,High,Integer).
%----------------------------------------------------------------------
remove_N(List,0,List).

remove_N([H|T],N,[H|RestOfList]):-
    N > 0,
    remove_N(T,N,RestOfList).

remove_N([_|T],N,List):-
    N > 0,
    N1 is N - 1,
    remove_N(T,N1,List).
%----------------------------------------------------------------------

Does anyone have a neater solution to suggest?

Cheers,

Geoff
--
Geoff Sutcliffe
Department of Computer Science    Email : geoff@cs.jcu.edu.au
James Cook University             Phone : +61 77 815085/814622
Townsville, Australia, 4811.      FAX   : +61 77 814029
