This predicate assembles a list of solutions of a goal according to a given pattern, findall(Object, Goal, List). The elements in the list are in order of solution.
The difference with bagof/3 is that all the objects are collected regardless of (possibly) different solutions for variables in Goal.
Assume the database contains the user-defined predicates:
legs(A, 6) :- insect(A). legs(A, 4) :- animal(A). legs(A, 8) :- spider(A). insect(bee) :- true. animal(horse) :- true. animal(dog) :- true. spider(tarantula) :- true. age(peter, 7). age(ann, 5). age(pat, 8). age(tom, 5).
see also: ^/2 bagof/3 setof/3 setof_reverse/3
| Examples | |
| findall(Child, age(Child, 5), List). | List = [ann, tom] |
| findall(Child, age(Child,Age), List). | Child = __1, Age = __2, List = [peter, ann, pat, tom] |
| findall(Child/Age, age(Child, Age), List). | List = [peter/7, ann/5, pat/8, tom/5] |
| findall(Child, Age ^ age(Child, Age), List). | List = [peter, ann, pat, tom] |
| findall(A, legs(A, N), List). | List = [bee, horse, dog, tarantula] |
| Exceptions | |
| Third argument is not a list or a variable. | a type_error(list, A) exception is thrown, A is replaced by the incorrect third argument |
| The second argument must not be an empty variable | an instantiation_error is thrown |
| The second argument is not a correct goal | a type_error(callable, A) exception is thrown, A is replaced by the incorrect goal |