🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #33

Re: How to pick elements out of lists?

#33
From: David Barker-Plummer
Date: Sun, 01 Nov 1992 14:18
49 lines
2290 bytes
>>>>> On 1 Nov 92 05:36:12 GMT, timm@spectrum.cs.unsw.oz.au (Tim
TM> Menzies) said: Nntp-Posting-Host: sienna.spectrum.cs.unsw.oz.au

In article <1992Nov1.053612.7414@usage.csd.unsw.OZ.AU> timm@spectrum.cs.unsw.oz.au (Tim Menzies) writes:

TM> From article <1992Oct29.140104.7751@coli.uni-sb.de>, by ulma@coli.uni-sb.de (Ulrich Mayring):
> Hello netters, I have a problem:
> 
> I have a list and need to recursively pick two elements out of it. I
> need every pair of elements in every order, however the order of the
> pair itself is not important. It should also be possible to pick an
> element and the [] as a pair, thus guaranteeing that in the end I
> will have every pair and every single element. Then I will test the
> pair (or single) and if it doesn't suit my purpose I want to go back
> into the recursion to get the next one.
> 
> 
> Thanx a lot if you can help,
> 
> 
>      Ulrich Mayring
> 
>        - Also wir waren mit grau eigentlich sehr zufrieden -     


TM> % no substitute for simplicity, i always say...

TM> twins(L,X) :- 	subset(L,X), (X = [_,_] | X = [_] | X = []).

TM> subset([],[]).
TM> subset([H|T],[H|REST]) :- subset(T,REST).
TM> subset([H|T],REST) :- subset(T,REST).   

Hi Tim,

On the other hand, if the set is large, then the amount of
backtracking that you will do will be horrendous, generating all
3,4,5,6,7,8... elements subsets, only to discard them later.

How about?

twins(L, pair(A,B)) :-
	member(A, L),
	member(B,L).
twins(L, pair(A,[])) :-
	member(A, L).

member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
Technical Details
Message-ID: <PLUMMER.92Nov1091801@nutmeg.cs.swarthmore.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!uwm.edu!linac!att!cbnewsk!cbnewsj!att-out!rutgers!psuvax1!news.cc.swarthmore.edu!plummer
References: <1992Oct29.140104.7751@coli.uni-sb.de> <1992Nov1.053612.7414@usage.csd.unsw.OZ.AU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!uwm.edu!linac!att!cbnewsk!cbnewsj!att-out!rutgers!psuvax1!news.cc.swarthmore.edu!plummer
From: plummer@cs.swarthmore.edu (David Barker-Plummer)
Newsgroups: comp.lang.prolog
Subject: Re: How to pick elements out of lists?
Message-ID: <PLUMMER.92Nov1091801@nutmeg.cs.swarthmore.edu>
Date: 1 Nov 92 14:18:01 GMT
References: <1992Oct29.140104.7751@coli.uni-sb.de>
 <1992Nov1.053612.7414@usage.csd.unsw.OZ.AU>
Sender: news@cc.swarthmore.edu (USENET News System)
Organization: Swarthmore College, Swarthmore, PA
Lines: 49
In-Reply-To: timm@spectrum.cs.unsw.oz.au's message of 1 Nov 92 05:36:12 GMT
Nntp-Posting-Host: nutmeg.cs.swarthmore.edu