Get product of the list items by using recursion in Prolog?











up vote
0
down vote

favorite












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?










share|improve this question
























  • product(, Res). says that the multiplication of no elements is anything you want. That doesn't make sense.
    – lurker
    Nov 10 at 23:44










  • What are you trying to achieve with mul(H, Res, X)?
    – lurker
    Nov 10 at 23:45










  • "mul is multiplication and it works fine" - it doesn't work fine for us unless you post the code. You should always provide a Minimal, Complete, and Verifiable example.
    – Enigmativity
    Nov 11 at 0:09










  • This is a problem: mul(H1, H2, Res), product(T, Res).. This is likely to fail unless the Res in the first expression is unifiable the Res in the second.
    – lurker
    Nov 11 at 0:53















up vote
0
down vote

favorite












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?










share|improve this question
























  • product(, Res). says that the multiplication of no elements is anything you want. That doesn't make sense.
    – lurker
    Nov 10 at 23:44










  • What are you trying to achieve with mul(H, Res, X)?
    – lurker
    Nov 10 at 23:45










  • "mul is multiplication and it works fine" - it doesn't work fine for us unless you post the code. You should always provide a Minimal, Complete, and Verifiable example.
    – Enigmativity
    Nov 11 at 0:09










  • This is a problem: mul(H1, H2, Res), product(T, Res).. This is likely to fail unless the Res in the first expression is unifiable the Res in the second.
    – lurker
    Nov 11 at 0:53













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question















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?







prolog successor-arithmetics






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:35









false

10.9k769141




10.9k769141










asked Nov 10 at 22:49









Ishmael Black

102




102












  • product(, Res). says that the multiplication of no elements is anything you want. That doesn't make sense.
    – lurker
    Nov 10 at 23:44










  • What are you trying to achieve with mul(H, Res, X)?
    – lurker
    Nov 10 at 23:45










  • "mul is multiplication and it works fine" - it doesn't work fine for us unless you post the code. You should always provide a Minimal, Complete, and Verifiable example.
    – Enigmativity
    Nov 11 at 0:09










  • This is a problem: mul(H1, H2, Res), product(T, Res).. This is likely to fail unless the Res in the first expression is unifiable the Res in the second.
    – lurker
    Nov 11 at 0:53


















  • product(, Res). says that the multiplication of no elements is anything you want. That doesn't make sense.
    – lurker
    Nov 10 at 23:44










  • What are you trying to achieve with mul(H, Res, X)?
    – lurker
    Nov 10 at 23:45










  • "mul is multiplication and it works fine" - it doesn't work fine for us unless you post the code. You should always provide a Minimal, Complete, and Verifiable example.
    – Enigmativity
    Nov 11 at 0:09










  • This is a problem: mul(H1, H2, Res), product(T, Res).. This is likely to fail unless the Res in the first expression is unifiable the Res in the second.
    – lurker
    Nov 11 at 0:53
















product(, Res). says that the multiplication of no elements is anything you want. That doesn't make sense.
– lurker
Nov 10 at 23:44




product(, Res). says that the multiplication of no elements is anything you want. That doesn't make sense.
– lurker
Nov 10 at 23:44












What are you trying to achieve with mul(H, Res, X)?
– lurker
Nov 10 at 23:45




What are you trying to achieve with mul(H, Res, X)?
– lurker
Nov 10 at 23:45












"mul is multiplication and it works fine" - it doesn't work fine for us unless you post the code. You should always provide a Minimal, Complete, and Verifiable example.
– Enigmativity
Nov 11 at 0:09




"mul is multiplication and it works fine" - it doesn't work fine for us unless you post the code. You should always provide a Minimal, Complete, and Verifiable example.
– Enigmativity
Nov 11 at 0:09












This is a problem: mul(H1, H2, Res), product(T, Res).. This is likely to fail unless the Res in the first expression is unifiable the Res in the second.
– lurker
Nov 11 at 0:53




This is a problem: mul(H1, H2, Res), product(T, Res).. This is likely to fail unless the Res in the first expression is unifiable the Res in the second.
– lurker
Nov 11 at 0:53












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I think you'll find that this does the trick:



product(, 0).
product([H], H).
product([H1, H2|T], Res) :-
mul(H1, H2, H),
product([H|T], Res).


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 call product/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.



%Addition
sum(0,M,M). %the sum of an integer M and 0 is M.
sum(s(N),M,s(K)) :- sum(N,M,K). %The sum of the successor of N and M is the successor of the sum of N and M.

%Multiplication
%Will work for mul(s(s(0)),s(s(0)),X) but not terminate for mul(X,Y,s(s(0)))
mul(0,M,0). %The product of 0 with any integer is 0
mul(s(N),M,P) :-
mul(N,M,K),
sum(K,M,P). %The product of the successor of N and M is the sum of M with the product of M and N. --> (N+1)*M = N*M + M





share|improve this answer





















  • Thank you. Sorry I forgot to add mul and sum.
    – Ishmael Black
    Nov 11 at 12:05










  • @IshmaelBlack - No worries.
    – Enigmativity
    Nov 11 at 22:10











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244189%2fget-product-of-the-list-items-by-using-recursion-in-prolog%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I think you'll find that this does the trick:



product(, 0).
product([H], H).
product([H1, H2|T], Res) :-
mul(H1, H2, H),
product([H|T], Res).


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 call product/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.



%Addition
sum(0,M,M). %the sum of an integer M and 0 is M.
sum(s(N),M,s(K)) :- sum(N,M,K). %The sum of the successor of N and M is the successor of the sum of N and M.

%Multiplication
%Will work for mul(s(s(0)),s(s(0)),X) but not terminate for mul(X,Y,s(s(0)))
mul(0,M,0). %The product of 0 with any integer is 0
mul(s(N),M,P) :-
mul(N,M,K),
sum(K,M,P). %The product of the successor of N and M is the sum of M with the product of M and N. --> (N+1)*M = N*M + M





share|improve this answer





















  • Thank you. Sorry I forgot to add mul and sum.
    – Ishmael Black
    Nov 11 at 12:05










  • @IshmaelBlack - No worries.
    – Enigmativity
    Nov 11 at 22:10















up vote
0
down vote



accepted










I think you'll find that this does the trick:



product(, 0).
product([H], H).
product([H1, H2|T], Res) :-
mul(H1, H2, H),
product([H|T], Res).


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 call product/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.



%Addition
sum(0,M,M). %the sum of an integer M and 0 is M.
sum(s(N),M,s(K)) :- sum(N,M,K). %The sum of the successor of N and M is the successor of the sum of N and M.

%Multiplication
%Will work for mul(s(s(0)),s(s(0)),X) but not terminate for mul(X,Y,s(s(0)))
mul(0,M,0). %The product of 0 with any integer is 0
mul(s(N),M,P) :-
mul(N,M,K),
sum(K,M,P). %The product of the successor of N and M is the sum of M with the product of M and N. --> (N+1)*M = N*M + M





share|improve this answer





















  • Thank you. Sorry I forgot to add mul and sum.
    – Ishmael Black
    Nov 11 at 12:05










  • @IshmaelBlack - No worries.
    – Enigmativity
    Nov 11 at 22:10













up vote
0
down vote



accepted







up vote
0
down vote



accepted






I think you'll find that this does the trick:



product(, 0).
product([H], H).
product([H1, H2|T], Res) :-
mul(H1, H2, H),
product([H|T], Res).


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 call product/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.



%Addition
sum(0,M,M). %the sum of an integer M and 0 is M.
sum(s(N),M,s(K)) :- sum(N,M,K). %The sum of the successor of N and M is the successor of the sum of N and M.

%Multiplication
%Will work for mul(s(s(0)),s(s(0)),X) but not terminate for mul(X,Y,s(s(0)))
mul(0,M,0). %The product of 0 with any integer is 0
mul(s(N),M,P) :-
mul(N,M,K),
sum(K,M,P). %The product of the successor of N and M is the sum of M with the product of M and N. --> (N+1)*M = N*M + M





share|improve this answer












I think you'll find that this does the trick:



product(, 0).
product([H], H).
product([H1, H2|T], Res) :-
mul(H1, H2, H),
product([H|T], Res).


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 call product/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.



%Addition
sum(0,M,M). %the sum of an integer M and 0 is M.
sum(s(N),M,s(K)) :- sum(N,M,K). %The sum of the successor of N and M is the successor of the sum of N and M.

%Multiplication
%Will work for mul(s(s(0)),s(s(0)),X) but not terminate for mul(X,Y,s(s(0)))
mul(0,M,0). %The product of 0 with any integer is 0
mul(s(N),M,P) :-
mul(N,M,K),
sum(K,M,P). %The product of the successor of N and M is the sum of M with the product of M and N. --> (N+1)*M = N*M + M






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 0:56









Enigmativity

74.4k764129




74.4k764129












  • Thank you. Sorry I forgot to add mul and sum.
    – Ishmael Black
    Nov 11 at 12:05










  • @IshmaelBlack - No worries.
    – Enigmativity
    Nov 11 at 22:10


















  • Thank you. Sorry I forgot to add mul and sum.
    – Ishmael Black
    Nov 11 at 12:05










  • @IshmaelBlack - No worries.
    – Enigmativity
    Nov 11 at 22:10
















Thank you. Sorry I forgot to add mul and sum.
– Ishmael Black
Nov 11 at 12:05




Thank you. Sorry I forgot to add mul and sum.
– Ishmael Black
Nov 11 at 12:05












@IshmaelBlack - No worries.
– Enigmativity
Nov 11 at 22:10




@IshmaelBlack - No worries.
– Enigmativity
Nov 11 at 22:10


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244189%2fget-product-of-the-list-items-by-using-recursion-in-prolog%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python