Visual Prolog Program to add the contents of an integer list.

Here in this program, the sum function is made that adds the contents of list. The first segment ( i.e the Domains section ) defines or declares the space dynamically for integer list. Then the predicate sum is defined that takes integer list and a Integer as arguments. The next clause section provides the necessary reasoning and facts to find the solution to the problem. Here the predicate sum is called recursively until it gets the last element of the list which is the empty list itself and returns 0 if empty list is encountered. The add predicate else adds the first element to the solution repeatedly to find the solution.

Source Code

DOMAINS
int_list = integer*

PREDICATES
sum(int_list, integer)

CLAUSES
sum([],0).
sum([H|T], X):-
sum(T,Y), X=H+Y.

GOAL
sum([1,8],Z).

SHARE Visual Prolog Program to add the contents of an integer list.

You may also like...

Leave a Reply

Your email address will not be published.

Share