Updated April 6, 2023
Introduction to Prolog findall
Prolog findall is a predicate function of the programming language to work with list, goal, and template. It is a metapredicate that works with a list of the templates and achieves the required goal. It is a built-in function to collect a list of the items using a template and bind the list to get a required goal. It is a function or method used to correlate objects of the programming language. It is a differentiated object subset as input and the remaining object as an output. It is a function to collect the list entities and bind them with other objects. It is a categories list of the entities only input, only output, input and output both.
Syntax
The prolog findall create list and object in the main file (main.pl).
The syntax of the prolog file is shown below:
content(merry, red).
content(mia, red).
content(henry, red).
content(Names, red) :- colors(Names).
colors(mia).
siblings(A,B):-content(A,C),content(B,C),A\=B.
The syntax of the prolog findall is shown below.
| ?- findall(A, content(A, red), Result).
The output object syntax is shown below.
| ?- siblings(A,B).
With file syntax is shown below.
count(M, [3, 5, 7, 9, 11]).
findall(M, count(M, [3, 5, 7, 9, 11]), Result).
With console syntax is shown below.
| ?- findall(M, count(M, [3, 5, 7, 9, 11]), Result).
How findall Works in Prolog?
Prolog comment works in the “pl” file.
Create a file with the “pl” extension.
Example:
main.pl
Create an object with values and modify them for prolog findall.
content(merry, red).
content(mia, red).
content(henry, red).
content(Names, red) :- colors(Names).
colors(mia).
The content name can be different like merry, mia, and henry.
The object subset must be the same as red. The prolog file is modified content values with another variable like colors (Names). The color function contains any variable value.
The other object uses to get input and output values as per command.
siblings(A,B):-content(A,C),content(B,C),A\=B.
The prolog file is modified content values with another variable like A and B. The sibling function contains content function values in another form. The A has represented the content names. The B is represented the other content names. Prolog comment works in the console Open prolog console or interpreter.
Set the directory path of the “pl” file.
Use the given prolog file.
[main].
Use the prolog findall syntax in the prolog console.
The syntax of the prolog findall is shown below.
| ?- findall(A, content(A, red), Result).
The output object syntax is shown below.
| ?- siblings(A,B).
With file syntax is shown below.
count(M, [3, 5, 7, 9, 11]).
findall(M, count(M, [3, 5, 7, 9, 11]), Result).
With console syntax is shown below.
| ?- findall (A, (content(A, [1,2,3,4])), Result).
Combine the prolog programming for the findall function.
In the file shown below.
content(merry, red).
content(mia, red).
content(henry, red).
content(Names, red) :- colors(Names).
colors(mia).
siblings(A,B):-content(A,C),content(B,C),A\=B.
In the console shown below.
| ?- findall(A, content(A, red), Result).
Examples
Given below are the examples mentioned:
Example #1
Code:
content(merry, red).
content(mia, red).
content(henry, red).
content(Names, red) :- colors(Names).
colors(mia).
In the console shown below.
Code:
| ?- findall(A, content(A, red), Result).
Output:
Example #2
Code:
content(merry, red).
content(mia, red).
content(henry, red).
content(Names, red) :- colors(Names).
colors(mia).
content(mia, yellow).
content(henry, yellow).
content(Names, yellow) :- colors(Names).
colors(mia).
In the console shown below.
Code:
| ?- findall(A, content(A, red), Result).
Output:
In the console shown below.
Code:
| ?- findall(A, content(A, yellow), Result).
Output:
Example #3
Code:
content(merry, red).
content(mia, yellow).
content(henry, red).
content(ram, red).
content(radha, black).
content(Names, red) :- colors(Names).
colors(ram).
siblings(A,B):-content(A,C),content(B,C),A\=B.
In the console shown below.
Code:
| ?- findall(A, content(A, red), Result).
Output:
Example #4
Code:
count(M, [3, 5, 7, 9, 11]).
findall(M, count(M, [3, 5, 7, 9, 11]), Result).
findall(M, (count(M, [3,4,5,6]), M < 7), Result).
In the console shown below.
Code:
| ?- findall(M, count(M, [3, 5, 7, 9, 11]), Result).
Output:
In the console shown below.
Code:
| ?- findall(M, (count(M, [3,4,5,6]), M < 7), Result).
Output:
Example #5
Code:
count(M, [3, 5, 7, 9, 11]).
findall(M, count(M, [3, 5, 7, 9, 11]), Result).
findall(M/n, (count(M, [3,4,5,6]), N is M * M), Result).
In the console shown below.
Code:
| ?- findall(M/n, (count(M, [3,4,5,6]), N is M * M), Result).
Output:
Example #6
Code:
content(merry, red).
content(mia, red).
content(henry, red).
content(adam, red).
content(jack, red).
content(sam, red).
content(alen, red).
siblings(A,B):-content(A,C),content(B,C),A\=B.
sisters(A,B):-content(A,C),content(B,C),A\=B.
In the console shown below.
Code:
| ?- siblings(A,B).
Output:
In the console shown below.
Code:
| ?- sisters(A,B).
Output:
Example #7
Code:
count(M, [ram, radha, yash, sadh, yuvi]).
findall(M, count(M, [ram, radha, yash, sadh, yuvi]), Result).
In the console shown below.
Code:
| ?- findall(M, count(M, [ram, radha, yash, sadh, yuvi]), Result).
Output:
Conclusion
The prolog findall uses to achieve goals using a list of the items in the application. It collects and binds the list templates in a single query and operates as per requirement. The multiple objects work in a short statement using the Prolog findall function.
Recommended Articles
We hope that this EDUCBA information on “Prolog findall” was beneficial to you. You can view EDUCBA’s recommended articles for more information.