Newbie question about homework assignment

Quote:

> Hi,

> I am doing my first Prolog program (a University assignment), so I am

> still quite stupid. Please forgive my ignorance :).

> Included at the end is the source (so far) of my program.

> Here is the given question:

> begin question -------------------------------------------------|

> 1. A, B, C, D, E, F and G stand for 7 consecutive integers

> * D is 3 less than A

> * B is the middle term

> * F is as much less than B as C is greater than D

> * G is greater than F

> What is the order of the variables?

> |-----------------------------------------------------end question

I would try to do it without using "->" at all.

Something like this :

num( 0 ).

num( 1 ).

num(2 ).

num( 3).

num( 4).

num( 5).

num( 6).

% Add more here if you wish.

solve_problem :-

num( A ), % A becomes a number

% up to number 6.

num( B ),

num( C ),

num( D ),

num( E ),

num( F ),

num( G ),

D is A - 3, % If not true then fail

% and get new values for A and D.

...

...

C_greater_than_D is C - D,

... is ...

... == C_greater_than_D,

G > F, % If not true then fail

% and get new values.

write( 'The numbers are : ' ), nl,

write( 'A = ' ), write( A ), nl,

write( 'B = ' ), write( B ), nl,

...

write( 'Thats all.' ).

solve_problem :-

write( 'Error, solve_problem failed. Maybe there is no solution. ' ),

nl.

If you need it to go up to large numbers,

then "num/1" facts are not suitable.

Ways to write a rule which generates numbers,

that you can use instead of "num/1",

have been discussed in this newsgroup,

about a year ago. Archives of this newsgroup

are available at Google Groups.

I'm afraid that I don't know what keyword to search on

to find this subject.

Don't expect to be able to solve this assignment fully,

it doesn't look easy to me.

--

Martin Sondergaard,

London.

Quote:

> My questions are these:

> -----------------------

> (A) Each one of the rules work correctly if you use actual numbers (not

variables), AND

> the facts in the Facts section are commented out.

> E.g. greater_than(3,2) -> 'yes'

> greater_than(2,3) -> 'no'

> BUT, with the facts included, the rules ALWAYS give a 'yes' reply (when

using real numbers).

Quote:

> E.g. greater_than(4,7). gives a 'yes'.

> (B) When I use uninitialised variables, I get errors, as I'm sure you

could predict.

> I don't think the variables are supposed to be given values either.

> So, how am I supposed to order uninitialised variables? Or do I HAVE to

initialise them?

Yes, you have to initialise them with something,

otherwise they will match anything,

and you can't get any useful result.

See "num/1" above as one way to initialise them.

Quote:

> Thanks for any help...

> begin source ------------------------------------------------------------

> % Rules

> % -----

> greater_than(X, Y) :- (X > Y).

> greater_than_by(X, Y, N) :- (X - Y) =:= N.

> less_than(X, Y) :- (X < Y).

> less_than_by(X, Y, N) :- (Y - X) =:= N.

> % Facts

> % -----

> greater_than(G,F).

> less_than_by(D,A,3).

> ----------------------------------------------------------------end source