Article View: comp.lang.prolog
Article #34Re: How to pick elements out of lists?
From: Ulrich Mayring
Date: Sun, 01 Nov 1992 21:23
Date: Sun, 01 Nov 1992 21:23
32 lines
1826 bytes
1826 bytes
ulma@coli.uni-sb.de (Ulrich Mayring) writes:
>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.
Well, since so many of you have answered, let me introduce the solution I
have finally decided on. Since I am a beginner it may not work in all circum-
stances, but it seems to in mine.
select(X,[X|R]),R).
select(X,[H|T],[H|R]) :- select(X,T,R).
select([],List,List).
Then my predicate goes something like select List A, select List B and then
append List A and List B. The last line in the select predicate appears to
generate the [] only when all other elements have been selected. When I
have the pairs I test them and if they suit my purpose I append them to my
"Solution List", which is the reason why it is possible to have a pair of
the form (List,[]) represent only List, which has one element.
Thanx again to everybody,
Ulrich Mayring
- Also wir waren mit grau eigentlich sehr zufrieden -
Technical Details
Message-ID:
<1992Nov1.212354.15732@coli.uni-sb.de>Path:
arc1-ca.pugleaf.net!sparky!uunet!math.fu-berlin.de!unidui!Germany.EU.net!sbsvax!coli-gate.coli.uni-sb.de!ulmaReferences:
<1992Oct29.140104.7751@coli.uni-sb.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!math.fu-berlin.de!unidui!Germany.EU.net!sbsvax!coli-gate.coli.uni-sb.de!ulma From: ulma@coli.uni-sb.de (Ulrich Mayring) Newsgroups: comp.lang.prolog Subject: Re: How to pick elements out of lists? Message-ID: <1992Nov1.212354.15732@coli.uni-sb.de> Date: 1 Nov 92 21:23:54 GMT References: <1992Oct29.140104.7751@coli.uni-sb.de> Organization: U of Saarbruecken, Dept. of Computational Linguistics Lines: 32