__findall/3 example:__

In Prolog, sometimes we would like to find all possible answers to a certain query, without using ' ; ', and other times we would also like to

use these answers within a list form to perform other operations on them. The

The ":Goal" here is a query that we are trying to satisfy. It could for example be as follows:

(member(X, [-1, 2, 9, -122, 0, 32, -23]), X<0 )

The "Template" variable is used to define, which variable in the :Goal are we trying to find all possible values for.

For example, in the query above, we are trying to find a negative number X within the list. To find all negative

numbers in the list, we would define the "Template" as the variable X in the

Finally, the "Bag" is a list of possible values the variable X can assume, i.e. the possible values of X

that can satisfy the query.

Lets run the query above and then compare it to

use these answers within a list form to perform other operations on them. The

*findall*predicate facilitates such a feature.*findall*has the following signature:*findall( +Template, :Goal, -Bag)*__Template :__The variable that could have alternate forms within the goal. The variable should be present within the goal.__Goal :__The actual query/goal that we are trying to satisfy.__Bag :__The list of alternatives that*Template*could unify with.The ":Goal" here is a query that we are trying to satisfy. It could for example be as follows:

(member(X, [-1, 2, 9, -122, 0, 32, -23]), X<0 )

The "Template" variable is used to define, which variable in the :Goal are we trying to find all possible values for.

For example, in the query above, we are trying to find a negative number X within the list. To find all negative

numbers in the list, we would define the "Template" as the variable X in the

*findall*predicate.Finally, the "Bag" is a list of possible values the variable X can assume, i.e. the possible values of X

that can satisfy the query.

Lets run the query above and then compare it to

*findall*predicate.?- member(X, [-1, 2, 9, -122, 0, 32, -23]), X<0 . X = -1 ; X = -122 ; X = -23.

As we can see, if I need to find all values of X at once, we need to tell Prolog to backtrack by using ' ; '. We can speed things up

by using

Template : X , because it is the variable we are trying to find all values for.

Goal : (member(X, [-1, 2, 9, -122, 0, 32, -23]) , X < 0 ) , since it is the query that includes X.

Bag: Xs , the variable name that will be assigned the list of values X can assume.

by using

*findall*as follows:Template : X , because it is the variable we are trying to find all values for.

Goal : (member(X, [-1, 2, 9, -122, 0, 32, -23]) , X < 0 ) , since it is the query that includes X.

Bag: Xs , the variable name that will be assigned the list of values X can assume.

?- findall(X, (member(X, [-1, 2, 9, -122, 0, 32, -23]), X<0), Xs) . Xs = [-1, -122, -23].

After running the query, Xs is now a list that contains all values X can be according to the query written within

can now be used for later operations.

*findall.*These valuescan now be used for later operations.