Author Message Newbie question about homework assignment

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

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).
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?

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

Sun, 17 Oct 2004 19:35:55 GMT  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
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.

- Show quoted text -

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

Mon, 18 Oct 2004 02:47:06 GMT  Newbie question about homework assignment
On Wed, 1 May 2002 19:47:06 +0100

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.

Those '->' were just for illustration purposes, in my message it is meant to mean
'returns' or 'prolog prints out'.
i.e.    greater_than(3,4) -> 'yes'
means
greater_than(3,4) prints out 'yes'

Also, I still don't know why, if, for e.g., you have a rule like
less_than(X,Y) :- X < Y.
and you have a fact like
less_than(A,B).
Prolog ALWAYS prints 'yes' for a query like
less_than(P,S).
no matter what values you put for P and S.

i.e.
less_than(4,5). prints 'yes'
AND
less_than(5,4). prints 'yes'.

Is it because the fact less_than(A,B) means that less_than is true for any value
because they are variables? Is there any way to get the behaviour I want?

That is the way we were told we're supposed to do it, but that just gives me problems.
What gives?

Thanks for any help.

Mon, 18 Oct 2004 16:18:30 GMT  Newbie question about homework assignment

Quote:
>Also, I still don't know why, if, for e.g., you have a rule like
>    less_than(X,Y) :- X < Y.
>and you have a fact like
>    less_than(A,B).
>Prolog ALWAYS prints 'yes' for a query like
>    less_than(P,S).
>no matter what values you put for P and S.

>i.e.
>    less_than(4,5). prints 'yes'
>            AND
>    less_than(5,4). prints 'yes'.

>Is it because the fact less_than(A,B) means that less_than is true for any value
>because they are variables?

that's right. A Prolog fact

less_than( A, B ).

asserted in the database means that a less_than/2 query can succeed
with any arguments.

Quote:
> Is there any way to get the behaviour I want?

Pass.

I'd guess instead of your "facts" you really want a "solve" predicate
something like:

solve( A, B, C, D, E, F, G ) :-
greater_than(G,F),
less_than_by(D,A,3),
....etc

Matt
--
Matthew Purver
matt-at-purver-dot-org

Mon, 18 Oct 2004 19:54:26 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages