With the help of a suitable example, describe the “member” function of PROLOG. How it can be used for searching of a data in a list, recursively.
In PROLOG, the "member" function is a built-in predicate that checks whether an element is a member of a given list or not. The syntax of the member function is as follows:
member(X, [X | _]).
member(X, [_ | T]) :- member(X, T).
The member function takes two arguments. The first argument, X, is the element we want to search for in the list. The second argument is the list that we want to search in. If X is a member of the list, the member function will return true, and if X is not a member of the list, the member function will return false.
Let's take an example to understand the working of the member function in PROLOG. Suppose we have a list of numbers, and we want to check whether a particular number is a member of that list or not. We can use the member function in PROLOG to achieve this.
Consider the following PROLOG code:
member(X, [X | _]).
member(X, [_ | T]) :- member(X, T).
% list of numbers
numbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).
% check if 4 is a member of the list of numbers
?- numbers(List), member(4, List).
In the above code, we first define the member function, which takes two arguments and checks if the first argument is a member of the second argument. Next, we define a list of numbers using the "numbers" predicate. Finally, we use the "member" function to check if the number 4 is a member of the list of numbers.
When we run the above code, we get the following output:
true.
This means that the number 4 is a member of the list of numbers, and the "member" function has returned true.
We can also use the member function to find all the elements of a list that satisfy a particular condition. For example, suppose we have a list of students with their marks in a particular subject, and we want to find all the students who have scored more than 80 marks.
Consider the following PROLOG code:
% student marks database
marks_database([
student1-85,
student2-60,
student3-90,
__________ ___ _____ __________ __________ _________ _________.
___ __________ _________ ____ _____ _________ ____ __________ ____ _________.
_______ ________ _______ _______ __________ ______ ________ ________ ________ _____ _______.
______ ______ __________ _______ ________ ___ _____ ______ ____.
____ ______ ____ ______ _______ _________ ________ ________ _______ _______ __________.
_____ _____ _________ ______ ______ __________ _________ __________.
__________ _____ ______ ______ ______ ________ _______ ______.
______ ___ ______ ___ _______ __________ _______ ____ _______ _________ _______ _____.
_____ _______ ____ _______ _______ _______ __________ __________ _______.
_____ _____ _________ __________ __________ ______ _______ ________ _______ ______.
__________ ____ __________ ____ _______ _________ __________ ___ __________.
_____ ________ ___ ______ _______ _______ ______ _________ __________.
____ ______ _______ ____ __________ __________ ____ ___ __________ _________ _______.
____ ______ ___ ___ __________ __________ ________ ____ ____ ___.
______ ______ ______ _____ ___ __________ _______.
__________ __________ _______ ___ ______ ______ _______ __________ ________ _______ ___.
_____ _______ ________ _______ ___ ________ ________.
________ _________ ____ _______ ___ ________ _________ _____ __________ __________ ______ __________.
_______ ________ ______ _____ ________ ___ __________ __________.
________ ______ ____ ______ __________ ______ ________.
____ __________ ____ _______ _____.
_______ ____ ___ _________ _____ _______ ___.
_____ ___ _____ __________ _________ _______ __________.
_______ _____ ___ ___ _________ _______ ________ ____ _________ _______ ____.
____ ____ __________ ________ ___ ______ __________ _____ _______ _________ ___ __________.
___ ___ ________ _____ _________ ________.
____ ___ ___ ___ _____ ______ ___ ___ _________.
__________ ________ _________ ____ ___ ________ _____.
_______ _______ __________ ___ ___ ___ _________ __________ ____.
_________ _________ ____ _____ __________ ___ ______ _________ __________.
__________ ____ ____ ___ ________ _________ ______ ___ ___ ___.
_________ _________ ___ _____ ________ _____ _________ __________ _______ __________ ________ ___.
________ __________ ____ ___ ___ _______ _________ ____ ______ ________ ______ ____.
_____ ______ __________ ______ ____ ______.
______ ________ ________ ____ _______ _______ ______ ________ _________ ______ __________ _____.
_____ ___ _________ _______ ______ __________ _________ ___.
_________ _____ _______ ________ ______ ____ ________ _________ ________ ________ ______ _________.
______ _____ ________ _________ ___.
_________ ___ __________ __________ ________ ___ ________ _______.
_________ ______ _____ __________ ___ ______ _________.
_________ _______ ______ _________ ________ _________ ____ _______ ______ _______ ______ ___.
_________ _______ __________ __________ ___ ______ _____ ________ ____ ______ _____.
___ _______ ___ ___ _______ ______ ____ ___ ______.
___ _______ _______ _________ ___ _______ ________.
_______ _____ _____ _____ _______ __________ __________ ______ ___ ____ ________ ___.
_________ _________ _____ ________ __________ ____ _________ ___ ____ _____ _____.
________ _____ ______ __________ _______ _______ _____ _____ ______.
_______ ________ _____ ___ _____.
_____ ______ ____ ____ _____ __________.
______ ________ ____ ________ ____ ________ ______ ___ __________ _______ ____.
__________ _____ ________ __________ ____ _____.
________ __________ _______ ________ ______ _________ _____ ________.
______ _______ _____ ______ ______ __________ _____ ________.
____ _____ ___ ____ ___ ______ _______ ___ ________ _________.
_______ _____ ____ _________ _______ ____ ___ _______.
___ ______ _______ ____ ___ __________ _________ _______.
_________ __________ ______ ___ ____ ________ ________ ___.
___ _____ ________ ___ _______ _________ _____ __________ _______ _______.
____ ______ _________ ___ _____ ___ __________ ______ _____.
_________ __________ ____ _____ __________ ________ _________ ________ _________ ____ _________ __________.
_______ ______ __________ _____ _____ ____.
____ _________ ___ _____ _____ ________ ______.
_________ _______ _______ ___ ____ ________ _________.
___ _____ ___ ____ ____ ______ ___ _____ _______ _____ __________ ______.
________ ________ _______ ___ ___ _______ ________ ________ __________ ___ _________.
_____ ________ _____ ____ _______ _______ __________ _______ __________ ______ _________.
____ _____ ________ ________ ________ __________ ___.
____ _____ ______ ______ _____ _________ ____ ____.
_________ _______ ___ ______ ____ _____ ____ _______.
___ _____ _________ ___ ___ ______ ____ _______ _____ ____.
____ ___ __________ ___ ____ ____ ___ ________ _______ ______ ______ ____.
_____ ______ _________ ____ ___ ___ ______ ___.
____ _____ ___ _____ _____ __________ ___ __________ ____ _______.
__________ ______ ____ __________ __________ _____ _________ _______ ___.
__________ ________ __________ __________ _____ ___ ________ _______ _____ ____.
_______ ___ ________ ____ _________ ________ ______ _________ _____.
_________ ________ _____ ________ _________ _________ _____ ___.
_________ __________ _______ _______ ______ _______ ______ _________ ______ _______.
________ ________ ______ ______ _________ ________ ________ ______ ___ ________.
________ _______ ________ ________ _________ _____ ______ ___ _______ ________.
________ ____ _______ ______ ________ ____ ____ _______ _________ __________ _______ ___.
________ ____ __________.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required