Article View: comp.lang.prolog
Article #40Re: How to pick elements out of lists?
From: Andreas Kagedal
Date: Mon, 02 Nov 1992 09:36
Date: Mon, 02 Nov 1992 09:36
72 lines
2945 bytes
2945 bytes
In article <PLUMMER.92Nov1091801@nutmeg.cs.swarthmore.edu> plummer@cs.swarthmore.edu (David Barker-Plummer) writes:
The original poster (Ulrich Mayring) wrote:
> 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 -
To this Tim Menzies replied:
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).
Of which David Barker-Plummer had the following to say:
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).
Now I say:
This will cause every pair to occur twice, since the order of the pair
was not important. A modified version:
twins(L, pair(A,B)) :-
member(A, L, RestOfL),
member(B, RestOfL).
twins(L, pair(A,[])) :-
member(A, L).
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
member(X, [X|Rest], Rest).
member(X, [_|T], Rest) :- member(X, T, Rest).
/Andreas
--
----------------------------------- < * > ------------------------------------
Andreas Kagedal Internet: andka@IDA.LIU.SE
Dep. of Computer and information science UUCP: {uunet,mcsun,...}!liuida!andka
University of Linkoping, Sweden BITNET: andka@SELIUIDA
Technical Details
Message-ID:
<ANDKA.92Nov2103650@aste12.IDA.LiU.SE>Path:
arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!liuida!curofix!andkaReferences:
<1992Oct29.140104.7751@coli.uni-sb.de> <1992Nov1.053612.7414@usage.csd.unsw.OZ.AU> <PLUMMER.92Nov1091801@nutmeg.cs.swarthmore.edu>Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.prolog Path: arc1-ca.pugleaf.net!sparky!uunet!mcsun!sunic!liuida!curofix!andka From: andka@IDA.LiU.SE (Andreas Kagedal) Subject: Re: How to pick elements out of lists? In-Reply-To: plummer@cs.swarthmore.edu's message of 1 Nov 92 14: 18:01 GMT Message-ID: <ANDKA.92Nov2103650@aste12.IDA.LiU.SE> Sender: news@ida.liu.se Organization: CIS Dept, University of Linkoping, Sweden References: <1992Oct29.140104.7751@coli.uni-sb.de> <1992Nov1.053612.7414@usage.csd.unsw.OZ.AU> <PLUMMER.92Nov1091801@nutmeg.cs.swarthmore.edu> Date: Mon, 2 Nov 1992 09:36:53 GMT Lines: 72