0
votes

I have a problem with a Prolog exercise. I'm trying to study Computer Science alone at my home, and I'm doing a series of exercises I've taken from the Internet.

The question is the following;

Construct a predicate called filteringPairsAtoms/3 so that given an atom (first argument), and a list of pairs, unify a third parameter with the filtered list of pairs by selecting only the pairs that have the first component as the atom of the first argument.

Example

filteringPairsAtoms(sA,[[basA,absAb],[ab,bbsA],[sA,abbsB],[bsA,sAsB],[sA,bb]],X)  

must result in

X = [[sA,abbsB],[sA,bb]]

I'm trying to solve it but I don't have anyone to ask, because I'm learning alone, and don't have a professor to write to or something.

Any help is welcome.

Thanks!!!

1
What have you tried so far? Please provide the steps that you have tried and any specific questions that you may have from that. - Seeta Somagani
I've used recursive search on the list, but i dont have any positive results yet. - Emily_1990
you can show us anything you got. any code, any attempt is OK. so we can see where's your difficulty. otherwise it's shooting in the dark. - Will Ness
@Emily_1990 It's a bit off-topic, but since you study on your own, you can first spend time on more structured and simple tasks like the classical Ninety-Nine Prolog Problems. Then you can add helpful details to your questions like "I don't understand this problem, but I do understand these and these simpler related problems" (and in this case even a rubber duck may help you no less than a professor.) - Kirill Bulygin
Adding to Kirill, there are a lot of great Prolog books for self-study, and the language hasn't changed much in a few decades, so even fairly old books can be helpful still. I would recommend Programming in Prolog by Clocksin & Mellish or Art of Prolog by Sterling & Shapiro. - Daniel Lyons

1 Answers

3
votes

You can start from what you already have:

filteringPairsAtoms(sA, [[basA,absAb],[ab,bbsA],[sA,abbsB],[bsA,sAsB],[sA,bb]], X) :-
    X = [[sA,abbsB],[sA,bb]].

That's as good a start as any. But we know more: it must be the case that

filteringPairsAtoms(sA, [[sA,abbsB],[bsA,sAsB],[sA,bb]], X) :-
    X = [[sA,abbsB],[sA,bb]].

%% and
filteringPairsAtoms(sA, [[bsA,sAsB],[sA,bb]], X) :-
    X = [[sA,bb]].

%% and
filteringPairsAtoms(sA, [[sA,bb]], X) :-
    X = [[sA,bb]].

%% and
filteringPairsAtoms(sA, [], X) :-
    X = [].

Comparing the sequential clauses, we also notice that it must be

filteringPairsAtoms(sA, [[sA,abbsB] | [[bsA,sAsB],[sA,bb]] ], X) :-
    sA = sA,
    X = [[sA,abbsB] | Y],
    filteringPairsAtoms(sA, [[bsA,sAsB],[sA,bb]], Y).

(this is just the first two clauses from above, combined); and

filteringPairsAtoms(sA, [[bsA,sAsB] | [[sA,bb]] ], X) :-
    dif( sA, bsA),
    X = Y,
    filteringPairsAtoms(sA, [[sA,bb]], Y).

All that's left for you to do, is generalize this by replacing concrete terms with logical variables. Like this:

filteringPairsAtoms(SA, [Head | Tail ], X) :-
    Head = [BsA, _]
    dif( SA, BsA),
    X = Y,
    filteringPairsAtoms(SA, Tail, Y).

and carry this out fully to the end.