CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@bohar • 22 Mar, 2011

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?

@Manish Goyal • 22 Mar, 2011 Please refer Logic an Prolog Programming by Saroj Kaushik

Page n0 180 😉
@Deepika Bansal • 25 Mar, 2011 Hehe that was a short and a sweet answer Manish.
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.😀
8.5k views

Related Posts

@Ankita Katdare · Jun 18, 2012

Here is a list of project ideas for projects of Civil Engineering. Students belonging to third year or final year can use these projects as mini-projects as well as mega-projects....
80.2k views

@syed.saifuddin · Oct 22, 2007

HI DEAR FRDS IAM SAIF IAM PERSUING MY ELECTRICAL AND ELECTRONIC ENGG FROM JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY FROM HYDERABAD. I NEED SOME ABOUT THE PROJECTS THAT CAN BE DONE IN...
5.6k views

@Ambarish Ganesh · Jun 6, 2013

Nissan has set yet another benchmark for its budget brand Datsun, where the company has recently announced via email a “Datsun World Premiere” on July 15th. Targeting the emerging markets...
4.5k views

@Ankita Katdare · Mar 7, 2015

As India is facing the burning question of whether or not the BBC Documentary 'India's Daughter' should have been banned or not, folks over at UST Global have taken some...
8k views

@Kaustubh Katdare · Dec 2, 2010

Let's say a phone has 8gb of internal memory and supports upto 32gb through microSD. Does that mean the phone will support total storage space of 8+32 = 40 GB?...
7.9k views