GodlessAstronomer
Lifer
I really suck at this kind of programming, so go easy on me 🙂 I'm clueless in Prolog.
I need to write a recursive program that can tell me whether or not X is greater than Y. The catch is that it also has to be able to list all values of X or Y if they are given as free variables.
So, greater_than(5, Y) should return 4, 3, 2, and 1. It needs to work for positive integers up to 20. So greater_than(X, 15) should return 16, 17, 18, 19, 20. It must use a recursive algorithm.
This is what I have come up with, which is obviously wrong.
This returns true and false values correctly but can't handle free variables. It's essentially the same as
which is obviously not very useful 🙂
Any help would be appreciated. This isn't homework, but it's a university lab assignment from earlier in the year and I'm studying for my exams.
I need to write a recursive program that can tell me whether or not X is greater than Y. The catch is that it also has to be able to list all values of X or Y if they are given as free variables.
So, greater_than(5, Y) should return 4, 3, 2, and 1. It needs to work for positive integers up to 20. So greater_than(X, 15) should return 16, 17, 18, 19, 20. It must use a recursive algorithm.
This is what I have come up with, which is obviously wrong.
Code:
one_greater_than(X,Y) :- X is Y+1.
greater_than(X,Y) :- one_greater_than(X,Y).
greater_than(X,Y) :- Z is X-1, Z > 0, greater_than(Z,Y).
This returns true and false values correctly but can't handle free variables. It's essentially the same as
Code:
greater_than(X,Y) :- X > y.
which is obviously not very useful 🙂
Any help would be appreciated. This isn't homework, but it's a university lab assignment from earlier in the year and I'm studying for my exams.