Thursday, June 21, 2012

Visual Prolog Program to find length of a list.

In this program the predicate length takes integer list and output a integer which is the length of list. Here the predicate length is called repeatedly or recursively until a last empty list is encountered and the 0 value is returned which is then continuously incremented to find the length.

Source Code

DOMAINS
int_list = integer*

PREDICATES
length(int_list, integer)

CLAUSES
length([],0).
length([H|T],L):-length(T,L1), L=L1+1.

GOAL
length([1,2,3,4,5],Z).

No comments:

Post a Comment