With the help of a suitable example, describe the “member” function of PROLOG. How Searching of a data in a list, recursively.

QuestionsWith the help of a suitable example, describe the “member” function of PROLOG. How Searching of a data in a list, recursively.
akankshat.ngPublished on: 4/19/2024 4:47:06 AM

With the help of a suitable example, describe the “member” function of PROLOG. How Searching of a data in a list, recursively?


1 Answers
Best Answer 0
Akanksha Tripathi 10/16/2018 1:25:00 AM

Member Function the function is used to determine whether a given argument X is a member of a given list L. Though the member function is a built-in function in almost every implementation of PROLOG, yet the following simple (recursive) program in PROLOG for member achieves the required effect: member(X, [X| _ ].

(i) member (X, [_ |Y]):- member (X, [Y]).

(ii) Definition of member under

(i) above says that if first argument of member is Head of the second argument, then predicate member is true. If not so, then, go to definition under

(ii). The definition of member under

(ii) above says that, in order to find out whether first argument X is a member of second argument then find out whether X is a member of the list obtained from the second argument by deleting the first element of the second argument. From the above definition, it is clear that the case member (X, [ ]) is not a part of the definition. Hence, the system returns FALSE. Next, we discuss example to explain how the PROLOG system responds to the queries involving the member function.

Example 3: -member (pascal, [prolog, fortran, pascal, cobol]) The PROLOG system first attempts to verify(i) in the definition of ‘member’, i.e., system matches pascal with the Head of the given list [prolog, fortran, pascal, cobol]. i.e., with prolog. The two constants are not identical, hence, (i) fails. Therefore, the PROLOG system uses the rule (ii) of the definition to solve the problem. According to rule (ii) the system attempts to check whether pascal is a member of the tail [fortran, pascal, cobol] of the given list. Again fact (i) of the definition is applied to the new list, i.e., [fortran, pascal, cobol] to check whether pascal belongs to it. As pascal does not match the Head, i.e, fortran of the new list. Hence, rule (ii) is applied to the new list. By rule(ii), pascal is a member of the list [fortran, pascal, cobol], if pascal is a member of the tail [pascal, cobol] of the current list.

Again fact (i) is applied to check whether pascal is a member of the current list [pascal, cobol]. According to fact (i) for pascal to be a member of the current list pascal should be Head of the current