0
votes

What I basically want to achieve is, that given a list of lists A, I want a predicate that checks if the elements of a list B are exactly contained in list A.

So for example:

A = [[1,2],[3,4],[5],[]] B = [1,2,3,4,5]

and

A = [[1,2],[3,4],[5],[]] B = [2,5,3,4,1]

Would result to true, but

A = [[1,2],[3,4],[5],[]] B = [1,2,3,4]

and

A = [[1,2],[3,4],[5],[]] B = [1,2,3,4,5,6]

would both result to false.

is this possible in prolog?

Exactly means: Order doesn't matter, it just has to contain all the elements. Also, imagine that the B list doesn't contain duplicates. So if A would contain duplicates, we should get false as a result.

1
Of course this is possible. In prolog, there are predicates, not functions. They're not really the same thing and don't have the same behavior. You should look at flatten/2 which will make this simple.lurker
Still not clear what exactly means, based on your examples. Does the order matter? What happens with repetitions?user1812457
append/2 could be and answerCapelliC
@lurker and Boris, thank you for your quick responses, I edited my question to give you a better description of my problem. Keep in mind that I am new to prolog, so there are no 'trivial' answers for me.voluminat0
This still doesn't make sense. Do you mean, "the set of elements in the nested list A is the same as the set of elements of list B"? Or maybe, "the stably sorted elements in nested list A is the same as the stably sorted list B"?user1812457

1 Answers

1
votes

The trivial answer:

?- flatten([[1,2],[3,4],[5],[]], [1,2,3,4,5]).
true.

?- flatten([[1,2],[3,4],[5],[]], [1,2,3,4]).
false.

?- flatten([[1,2],[3,4],[5],[]], [1,2,3,4,5,6]).
false.

Or,

foo(A, B) :- % because I don't know how to call it
    flatten(A, B).

If you are talking about sets:

bar(A, B) :-
    flatten(A, A_flat),
    sort(A_flat, A_sorted),
    sort(B, A_sorted).

You can use msort/2 if you don't want to remove duplicates.

If the question is, "how do I implement flatten/2", you can find several answers on SO.