Re: List operations-Stable Marriage


Tim Finin (finin@cs.umbc.edu)
Thu, 06 Mar 2003 10:27:29 -0500


anjali1 wrote:
> The list of pairings for the couples if expressed as say
> [adam+eve,tom+mary...], then how does one perform list operations on such a
> list? I mean, how to access the just adam or eve or tom etc from the list? As
> in how does one access the first or the last element of all the lists in the
> couples lists? Please suggest.

Here's a predicate that constructs a list of the men
in the list of couples where a couple is represented
by the term TheMan+TheWoman.

   % men(+Couples,-Men) where Couples is a list of couples and
   % each couple is of the form Man+Woman and Men is a list of the
   % men in the couples.
   men([],[]).
   men([AMan+_|MoreCouples],[AMan|MoreMen]) :-
      men(MoreCouples,MoreMen).

An equivalent way to write this could be as follows.

   men([],[]).
   men(Couples,Men) :-
     [AMan+_|MoreCouples]=Couples,
     men(MoreCouples,MoreMen),
     Men=[AMan|MoreMen].

This points out one of the neat things about unification
in prolog (and in general) -- it doesn't matter when you
do it.



This archive was generated by hypermail 2.0b3 on Thu Mar 06 2003 - 10:27:43 EST