How do you get a product of the list items by using recursion?
If I ask:
product([s(0), s(s(0)), s(s(0))], S).
The result should be:
S = s(s(s(s(0)))).
But I geep geting wrong results. Or no results.
I tried:
product([], 0).
product([], Res).
product([H1, H2|T], Res) :- T\=[], mul(H1, H2, Res), product(T, Res).
product([H|T], Res) :- mul(H, Res, X), product(T, X).
mul is multiplication and it works fine.
If I use trace i can see that it finds th result but than it failes for some reason.
Call: (10) product([], s(s(s(s(0))))) ? creep
Fail: (10) product([], s(s(s(s(0))))) ? creep
Any idea anyone?
I think you'll find that this does the trick:
The first predicate is a trivial base case.
The second is where the list only contains a single element - so that's the answer.
The third is where you have a list of two or more elements - simply perform the
mul/3
on the first two and then recursively callproduct/2
. This will eventually match predicate 2 and complete.Your sample input does yield
s(s(s(s(0))))
.Please in the future include the definition for
mul/3
. I had to search the internet to find one.