• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need some simple Prolog help.

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.
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.
 
Back
Top