# Intersection\_chk/2

**URL:** <https://swi-prolog.discourse.group/t/intersection-chk-2/4081>\
**Category:** Nice to know\
**Tags:** on-topic-only, list, set, ordered-set\
**Created:** [June 16, 2021, 12:43pm UTC](https://swi-prolog.discourse.group/t/intersection-chk-2/4081 "2021-06-16T12:43:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [June 16, 2021, 12:43pm UTC](https://swi-prolog.discourse.group/t/intersection-chk-2/4081/1 "2021-06-16T12:43:13Z")

</div>

As Jan W. notes [below](https://swi-prolog.discourse.group/t/intersection-chk-2/4081/2), one should use [ordered sets](https://www.swi-prolog.org/pldoc/doc/_SWI_/library/ordsets.pl) and then use ord\_intersect/2 instead of the following code. 😵

```prolog
intersection_chk(A,B) :-
    member(Item,A),
    memberchk(Item,B), !.

```

* * *

Many Prolog programmers know of member/2 and less know of memberchk/2.  
Some know of intersection/3 and ord\_intersection/3 but there is no intersection\_chk/2, thus posted.

In checking thousands of sets against each other there was no need for the full intersection but only the fact if the two sets have at least one item in common. I think of this as a [short circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation), why do all of the checks if you can stop when you find one that succeeds.

As I have often noted, correctness comes before performance. In this case to get a correct result requires converting the list to ordered sets using list\_to\_ord\_set/2 and then using ord\_intersect/2 instead of the posted code above.

> **Test cases**
>
> ```prolog
> :- begin_tests(my_set).
> 
> :- use_module(my_set).
> 
> intersection_chk_test_case(01,[],[],false ).
> intersection_chk_test_case(02,[a-1],[],false ).
> intersection_chk_test_case(03,[],[a-1],false ).
> intersection_chk_test_case(04,[a-1],[a-1],true ).
> intersection_chk_test_case(05,[a-1],[b-1,c-1,a-2],false ).
> intersection_chk_test_case(06,[a-1,b-2,c-2,d-1],[b-1,c-1,a-2],false ).
> intersection_chk_test_case(07,[a-1,b-2,c-2,d-1],[a-1,c-1,a-2],true ).
> intersection_chk_test_case(08,[a-1,b-2,c-2,d-1],[b-1,a-1,a-2],true ).
> intersection_chk_test_case(09,[a-1,b-2,c-2,d-1],[b-1,c-1,a-1],true ).
> intersection_chk_test_case(10,[a-1,b-2,c-2,d-1],[b-2,c-1,a-2],true ).
> intersection_chk_test_case(11,[a-1,b-2,c-2,d-1],[b-1,b-2,a-2],true ).
> intersection_chk_test_case(12,[a-1,b-2,c-2,d-1],[b-1,c-1,b-2],true ).
> intersection_chk_test_case(13,[a-1,b-2,c-2,d-1],[d-1,c-1,a-2],true ).
> intersection_chk_test_case(14,[a-1,b-2,c-2,d-1],[b-1,d-1,a-2],true ).
> intersection_chk_test_case(15,[a-1,b-2,c-2,d-1],[b-1,c-1,d-1],true ).
> 
> test(intersection_chk,[true,forall(intersection_chk_test_case(_Index,A,B,true))]) :-
> intersection_chk(A,B).
> 
> test(intersection_chk,[fail,forall(intersection_chk_test_case(_Index,A,B,false))]) :-
> intersection_chk(A,B).
> 
> :- end_tests(my_set).
> 
> ```

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 16, 2021, 1:00pm UTC](https://swi-prolog.discourse.group/t/intersection-chk-2/4081/2 "2021-06-16T13:00:41Z")

</div>

As the example at intersection/3 named _What is wrong with intersection/3 and friends?_ explains, the set manipulation based on unification is dubious. If you need sets, use library(ordsets). That has ord\_intersect/2 which just tests that two sets intersect.

Alternatively you may choose for hash tables balanced trees to represent sets, depending non what you need to do with them.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [June 16, 2021, 1:07pm UTC](https://swi-prolog.discourse.group/t/intersection-chk-2/4081/3 "2021-06-16T13:07:18Z")

</div>

Should we leave this topic up or delete the entire thing and I can post a new topic in [Nice to know](https://swi-prolog.discourse.group/c/nice-to-know/17)?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 16, 2021, 1:09pm UTC](https://swi-prolog.discourse.group/t/intersection-chk-2/4081/4 "2021-06-16T13:09:51Z")

</div>

I more extensive discussion on how to deal with sets is more useful. An intersectionchk/2 is useful if you do sets the wrong way 🙂
