Tuesday, 27 May 2014

Prolog Program to find the length of the list

Prolog Program to find the length of the list

Program:

domains
    list=symbol*
 
predicates
    len(list)
    findlen(list,integer)
 
clauses
    len(X) :- findlen(X,Count), write("\nLength Of List : "), write(Count).
    findlen([],X) :- X=0.
    findlen([X|Tail],Count) :- findlen(Tail,Prev), Count = Prev + 1.
        

goal with output

goal: len([a,b,c,d,e])
output: Length Of List : 5True

1 comment: