`findall/[3,4]`

[ISO]`findall(`

`+Template``, `

`+Generator``, `

`-List``)`

`findall(`

`+Template``, `

`+Generator``, `

`-List``, `

`+Remainder``)`

`List` is the list of all the instances of `Template` for
which the goal `Generator` succeeds, appended to `Remainder`.
`Remainder` defaults to the empty list.

`Template`- term
`:Generator`- callable, must be nonvar
A goal to be proved as if by

`call/1`

. `List`- list of term
`Remainder`- list of term

A special case of `bagof/3`

, where all free variables in the
generator are taken to be existentially quantified, as if by means of
the ``^`' operator. Contrary to `bagof/3`

and `setof/3`

,
if there are no instances of `Template` such that
`Generator` succeeds, then `List` = `Remainder`.

Because `findall/[3,4]`

avoids the relatively expensive variable
analysis done by `bagof/3`

, using `findall/[3,4]`

where
appropriate rather than `bagof/3`

can be considerably more
efficient.

`bagof/3`

can succeed nondeterminately, generating alternative
values for `Set` corresponding to different instantiations of the
free variables of `Generator`.

Call errors (see ref-sem-exc).

To illustrate the differences among
`findall/3`

, `setof/3`

, and `bagof/3`

:

| ?-[user].|foo(1,2).|foo(1,2).|foo(2,3).| % user compiled in module user, 0.100 sec 352 bytes yes | ?-bagof(X, foo(X,Y), L).X = _3342, Y = 2, L = [1,1];X = _3342, Y = 3, L = [2];no

| ?-bagof(X, Y^foo(X,Y), L).X = _3342, Y = _3361, L = [1,1,2];no

| ?-findall(X, foo(X,Y), L).X = _3342, Y = _3384, L = [1,1,2];no

| ?-setof(X, foo(X,Y), L).X = _3342, Y = 2, L = [1];X = _3342, Y = 3, L = [2];no

`bagof/3`

, `setof/3`

, `^/2`

, ref-all.

Send feedback on this subject.