%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Define allweight(L, W), such that it is true iff
% W is the total weight of all the animals in list L.
% Assume that the weight of the animals is stored in
% predicates of the form weight(X, N).

weight(bird, 1).
weight(dog, 20).
weight(bear, 300).
weight(elephant, 1000).

allweight([],0).
allweight([X],W):- weight(X,W).
allweight([H|T],W):- allweight(T,Sum1),weight(H,Sum2),W is Sum1 + Sum2.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *