Updated April 15, 2023
Introduction to Prolog length
Prolog length is a function of the list to calculate data length with a method of the programming language. It is a method to find the length of the list using a programming language. It is an operator to represent the number of list entities using the Prolog language. The prolog list displays the length of the list entities and predicates the relation of the list. It is determined and calculates the available object inside the list function. It is used to operate the list data of the database using a prolog programming language. It is a method to maintain list values and their operations such as an increment and decrement.
Syntax of Prolog length
The prolog length needs some method, objects, and values in the pl files. The prolog length in pl file syntax is shown below.
\value_length([],0).
value_length([_|END], A) :-
value_length(END, A1), A is A1+1.
Explanation:
- The value_length is a method to contain attributes.
- The “A” and “A1” are the attributes.
- The “END” is a tail of the prolog list.
- The list displays the head of the list before the “END” method.
Console syntax is shown below.
value_length([value1, value2, value3], Result_value).
With tail in console, syntax is shown below.
value_length([value1, value2, value3| TAIL], Result_value).
Console syntax is shown below.
length( [value1, value2, value3], Result_value).
With empty list, syntax is shown below.
length( [ ], Result_value).
Explanation:
- The value_length is a method included in the pl file.
- This method name might be the same in the pl file and prolog console.
- The prolog console can use the “length” keyword for the method.
- You can find the length of the list directly in the console.
- If you include the list in the length method then the pl file does not use for reference.
How does length Work in Prolog?
Prolog comment works in the “pl” file. Create a file with the “pl” extension.
Example: main.pl
Insert data into the prolog file.
Save the object with value into a pl file.
The following commands can use for the data of the list.
- Initialize the list data and length with objects.
value_length([],0).
- It contains the start and remains the data object in the list.
value_length([_|END], A)
- The head and tail value of the list inserts into the pl file.
- The result set object is placed inside of the method.
value_length([_|END], A) :-
value_length(END, A1), A is A1+1.
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].
- You can use the prolog console directly for programming.
- Use the syntax in the prolog console.
| ?- value_length( [1, 2, 3], Result_value).
- Use the syntax in the prolog console.
| ?- value_length( [1, 2, 3|A], Result_value).
- Use with empty list syntax in the prolog console.
| ?- value_length( [ ], Result_value).
- Use with length keyword syntax in the prolog console.
| ?- length( [1, 2, 3|A], Result_value).
Combine the working procedure of the function.
- Use the “main.pl” file.
value_length([], 0).
value_length([_|END], A) :-
value_length(END, A1), A is A1+1.
- Use the given prolog console with the required directory path.
[main].
- Use the syntax in the prolog console.
| ?- value_length( [1, 2, 3, 4, 5, 6, 7], Result_value).
Or
| ?- value_length( [a, b, c, d, e, g, h|A], Result_value).
Or
| ?- length( [1, 2, 3, 4, 5, 6, 7, 8], Result_value).
Examples of Prolog length
Given below are the examples mentioned:
Example #1
The basic example and output are shown below.
The prolog (pl) file:
value_length([],0).
value_length([_|END], A) :-
value_length(END, A1), A is A1+1.
Code:
| ?- value_length( [a, b, c, d, e, f, g |A], Result_value).
Output:
Explanation:
- The data list uses the character values for operation.
- Its function displays the length of the prolog list entities.
- The list data contain character values with the head of the list.
- The result set object is used at the end of the list.
Example #2
The basic example and output is shown below.
The prolog (pl) file:
value_length([],0).
value_length([_|END], A) :-
value_length(END, A1), A is A1+1.
Code:
| ?- value_length([a, b, c, d, e, f, g ], Result_value).
Output:
Explanation:
- The data list uses the character values for operation.
- The function displays the length of the prolog list entities.
- The list data contain character values without the head of the list.
- The result set object is used at the end of the list.
Example #3
The basic with length keyword example and the output show below.
The prolog (pl) file:
value_length([],0).
value_length([_|END], A) :-
value_length(END, A1), A is A1+1.
Code:
| ?- length( [a, b, c, 1, 5, 7, d, e, f, g |A], Result_value).
Output:
Explanation:
- The data list uses the character and numerical values for operation.
- The function displays the length of the prolog list entities.
- The list data contain character values with the head of the list.
Example #4
The basic with numerical data list example and output are shown below.
The prolog (pl) file:
value_length([],0).
value_length([_|END], A) :-
value_length(END, A1), A is A1+1.
Code:
| ?- length( [1, 2, 3, 4, 5, 6, 7], Result_value).
Output:
Explanation:
- The data list uses the numerical values for operation.
- The function displays the length of the prolog list entities. The list data contain character values without the head of the list.
Conclusion
It uses to operate and maintain the large size data and list. It helps to update data size and their operations. It is a function to get the information about the data list.
Recommended Articles
We hope that this EDUCBA information on “Prolog length” was beneficial to you. You can view EDUCBA’s recommended articles for more information.