0
votes

I've been trying for a long time and searched a lot (...) but nothing.

What I want is to create a function that will compare elements of one list to the elements of other list.

The things is I have absolutely no idea how I'm going to iterate through the members of both lists.

Ex. comparing the first member of the first list L1 to the members of L2. If it meets a certain condition (the condition may vary, but compare/4, member and such won't do), I'll pick the second member of L1 to all the members of L2, and so on.

The thing is I don't know how to get back the "full list" L2 after checking through its members while comparing to the first element of L1 by using [P|R]. After that how will I compare the second element of L1 to the elements of L2? Any way to "reset" it back to the first position?

I had something like

function([P|R], [F|L]) :-
   P == F,
   function([P|R],L)
  ;
   P \== F, (guess not necessary)
   function(R, [F|L]).

(it has more stuff but this is pretty much the basic of what I want)

But it exhibits odd behaviour after going through all the members of 1 list.

And yes, it's probably a very basic question but Prolog is just not something I've gotten used to..

1
" searched everywhere " ? I doubt it... - CapelliC
Guess not the entire world... But all I find is basic questions on membership from 99% of the results.. - user3657765
Try to get used to using correct Prolog terminology. It doesn't have function which take inputs, compute, and produce outputs, but rather predicates which define relations between entities and instantiate variables in such a way as to make that relation succeed (be true). - lurker
It's hard to tell what you want. A clear problem statement, sample code showing us what you're trying to accomplish, amd sample data and expected/desired results would be a big help in helping us to help you. - Nicholas Carey

1 Answers

3
votes

If you have a list whose elements you are comparing in some way to all of the elements of another list, and then having a resultant list, then you need at least 3 arguments.

compare_list([H1|T1], L, R) :-
    (   check_element(H1, L),     % Compare H1 with elements of L somehow
    ->  R = [H1|T2]               % if the criteria succeeds
    ;   R = T2                    % if the criteria fails
    ),
    compare_list(T1, L, T2).
compare_list([], _, []).

You mentioned that you are comparing the elements of the first list with those of the second somehow, but that this may vary. So I just called this a predicate, check_element/2 which accepts the element from the first list and an element from the second list and compares them in some way.

check_element(X, [H|T]) :-
    ... % compare X and H somehow; decide how/when to succeed
    check_element(X, T).
check_element(_, []).

The behavior of check_element/2 can vary as you wish. It might succeed before it completes going through the list [H|T] after hitting some criteria, or it may require that an element compare favorably somehow with every element of [H|T] before succeeding. Your choice. If it's the latter, you can replace check_element/2 with a form of maplist.