yeah I am too inexperienced for that- can you explain the simpler one first
printN(N) :- N > 0, M is N - 1, printN(M), writeln(N).
why is it 1 2 3 and not 3 2 1 I don't understand
no sir I do not. Ok, in my attempt to understand prolog recursion I picked up a nice pdf book about prolog, which had this example
printN(0).
printN(N) :- N > 0, M is N - 1, printN(M), writeln(N).
?- printN(3).
1
2
3
Yes
What I don't understand is, if initially M is N, and then we begin to...
sorry I don't quite understand that expression (I think it's only in french)- do you mean did I try to change it in some way to make it work? yeah.
I've tried changing the code like so
sum(N, D, Res) :- D<N, N1 is N - D,Res is Res1+N1, sum(N1, D, Res1).
but that only made it worse
well, the assigment said that if the inputs where D>=N then Res should equal N
so my full code looks like so
sum(N, D, Res) :- D<N, N1 is N - D, sum(N1, D, Res1), Res is Res1+N1.
sum(N, D, Res) :- D >= N, Res = N.
hi! I need to find the sum of all positive numbers up to N, with the step of D, for instance id D=3 and N=11 then 11+8+5+2=26. Currently my way is like so:
sum(N, D, Res) :- D<N, N1 is N - D, sum(N1, D, Res1), Res is Res1+N1.
however this yields the answer 17 which is incorrect please help!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.