![]() Predecessor(P,R):- parent(P,Q),predecessor(Q,R). In this way, we can move up to n number of levels. If P is the parent of Q then obviously P can be treated as a predecessor of Q, but if we go one step that means if P is the parent of Q and Q is the parent of R then we can say that P is the predecessor of R, similarly, if P is the parent of Q1 and Q1 is the parent of Q2 and Q2 is the parent of R then P can be called as the predecessor of R. As we have already defined parent, male, mother, father, haschild, brother, sister, grandparent, wife, and uncle family relationship and now we will see predecessor relationship. So in other programming languages, we might have used recursion and prolog supports recursion and here in a family relationship, we can use recursion. Let us see the working of recursion in family relationship, so we know that what is recursion, recursion is having mainly two issues, first is that for starting inputs and outputs are known to us so it is known as base case recursion, in prolog it will be satisfied by the given facts and another issue is that a prolog program or the prolog function can call itself, so we will see when the prolog calling itself in its declaration, so this can also a good example of recursion. Now if we say ‘is_eating(nonveg, food)’ this will be true if ‘is_eating(nonveg, food):- just_cook(nonveg, veg), is_eating(veg, food)’, then the statement ‘is_eating(nonveg, food)’ is also true. So, this type of predicates is called recursive, so if we say that ‘just_cook(veg, food)’ it means that ‘is_eating(veg, food)’ is true. ![]() Is_eating(S, T):- just_cook(S, R),is_eating(R, T). We will see the working of recursion with the help of one example, The predicate we are using in recursion is recursive in nature. It can also be used by many data structures so recursive data structure is the one where the structures include the sub-structures whose function is the same as that of the whole structure. So we wish to perform some operations either over data structure or until a certain point to be reached, in prolog this repetition is called recursion. The recursion is a very powerful tool in prolog, recursion is used by many programming languages. Rule: The rule is a type of form, it has been used to call the predicates and as we know the predicates are in-built in prolog.įor example, we have the clause that ‘Brain:- Heart.’, so it means ‘Brain is true if Heart is true’.Facts: The clauses without a body is called facts,.The clauses are divided into facts and rules. So, the first line as the parent(M, O) has some facts given from there it will get instantiated, which is also called a base case, the second line shows the recursive relationship.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |