Get product of the list items by using recursion in Prolog?
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
add a comment |Â
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
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 withmul(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 theRes
in the first expression is unifiable theRes
in the second.
â lurker
Nov 11 at 0:53
add a comment |Â
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
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
prolog successor-arithmetics
edited Nov 11 at 12:35
false
11.1k770142
11.1k770142
asked Nov 10 at 22:49
Ishmael Black
174
174
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 withmul(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 theRes
in the first expression is unifiable theRes
in the second.
â lurker
Nov 11 at 0:53
add a comment |Â
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 withmul(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 theRes
in the first expression is unifiable theRes
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
add a comment |Â
1 Answer
1
active
oldest
votes
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
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
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
add a comment |Â
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
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
add a comment |Â
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
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
answered Nov 11 at 0:56
Enigmativity
75k864129
75k864129
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
add a comment |Â
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
add a comment |Â
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid â¦
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 theRes
in the first expression is unifiable theRes
in the second.â lurker
Nov 11 at 0:53