if there is list L like [1,4,5,23,4,43] then what are rules or procedure required to count the element of list using recursion in prolog?
Page n0 180 😉
But let me give the facts so that it may proved beneficial for other CEans as well.
/* Let L be the list (represented as [H|T] ) that stores the elements to be counted. Let N be the number of elements in the list. count(L,N)- succeeds if N is unified (matched) with the length of the list L. */ count(,0). count([H|T],N) :- count(T,N1) , N is N1+1.Explanation: The recursive method needs one boundary statement (fact 1) and another recursive call (fact 2).
The recursive call will be made to the tail 'T' of the list L and the count variable Ni's are stored in the stack meanwhile. This will continue until it reaches the boundary condition ie when the list becomes empty. Then the count variable will be instantiated to 0 and corresponding Ni's will be calculated.
At the end the final result will be displayed by the prolog compiler.😀
@Ankita Katdare · Jun 18, 2012
@syed.saifuddin · Oct 22, 2007
@Ambarish Ganesh · Jun 6, 2013
@Ankita Katdare · Mar 7, 2015
@Kaustubh Katdare · Dec 2, 2010