# Nuances and usage tips on findall/4?

**URL:** <https://swi-prolog.discourse.group/t/nuances-and-usage-tips-on-findall-4/863>\
**Category:** Help!\
**Created:** [June 26, 2019, 7:49pm UTC](https://swi-prolog.discourse.group/t/nuances-and-usage-tips-on-findall-4/863 "2019-06-26T19:49:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prodog](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@prodog](https://swi-prolog.discourse.group/u/prodog)\
**Post date:** [June 26, 2019, 7:49pm UTC](https://swi-prolog.discourse.group/t/nuances-and-usage-tips-on-findall-4/863/1 "2019-06-26T19:49:39Z")

</div>

I’m using: SWI-Prolog version 8.0.2.

[https://www.swi-prolog.org/pldoc/doc\_for?object=findall/4](https://www.swi-prolog.org/pldoc/doc_for?object=findall/4)

First, I want to make sure I understand findall/4. Is it accurate to say that when you pass findall/4 a list of solutions in the _-Bag_ argument, it returns all the solutions in _+Tail_ that are not already in _-Bag_?

If so, then would one use _findall/4_ to get a new set of solutions you have not already tried? For example, perhaps you have an app where new solutions become available over time and you only want to try new solutions?

Are there any other creative uses of _findall/4_ that would be useful to know about?

---

<div class="post-metadata">

**Author:** ![jamesnvc](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jamesnvc/32/14_2.png) [@jamesnvc](https://swi-prolog.discourse.group/u/jamesnvc)\
**Post date:** [June 26, 2019, 7:57pm UTC](https://swi-prolog.discourse.group/t/nuances-and-usage-tips-on-findall-4/863/2 "2019-06-26T19:57:00Z")

</div>

I think the idea is that if you want to build a list from the end, you want to use a difference list to make that efficient, and not have to use append.

For example,

```prolog
% ugly/inefficient way
?- findall(X, member(X, [1,2,3,4]), S1),
    findall(X, member(X, [a,b,c,d]), S2), 
    append(S1, S2, Solns).
S1 = [1, 2, 3, 4],
S2 = [a, b, c, d],
Solns = [1, 2, 3, 4, a, b, c, d].

% using difference list + findall/4 - no append
?- findall(X, member(X, [1,2,3,4]), Solns, Tail),
    findall(X, member(X, [a,b,c,d]), Tail, []).
Solns = [1, 2, 3, 4, a, b, c, d],
Tail = [a, b, c, d].

```

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [June 26, 2019, 9:39pm UTC](https://swi-prolog.discourse.group/t/nuances-and-usage-tips-on-findall-4/863/3 "2019-06-26T21:39:45Z")

</div>

Good example by @jamesnvc. The following tests for the predicate may also help clarify its semantics:

[https://github.com/LogtalkDotOrg/logtalk3/blob/52e15bb251c0dd650241e344dd8786fcd6fada0d/tests/prolog/predicates/findall\_4/tests.lgt](https://github.com/LogtalkDotOrg/logtalk3/blob/52e15bb251c0dd650241e344dd8786fcd6fada0d/tests/prolog/predicates/findall_4/tests.lgt)
