1
votes

I will be honest and preface this by saying it is homework, but I desperately need help.

I am new to Prolog, coming from C++ and Javascript and no matter how hard I try, I'm just not getting it.

What I am supposed to do is to be able to find the difference between two dates (assuming non leap year).

Basically I have a knowledge base with information laid out like:

   object(A, B).
   object(A, B).
    ...

where A represents month and B represents total days of the month. (eg. object(1, 31).)

The expression I am supposed to use for input is this: Difference(Object(A,B), object(A,B), N). (N = difference between the objects).

I have tried a few different things (and I know this isn't on the right track) and haven't been able to get further then:

difference(object(A,B), object(A,B),N):-
    days(M,D), days(M,D),
    Y = 365-D,
    N is Y.

I don't understand how to make the two objects register as individual things that I can manipulate and therefore continue with the problem.

Can someone please please please point me in the right direction?

Thanks

Homework Question (AS REQUESTED). Assume the presence of the days relation that describes how many days are in each calendar month of a non-leap year.

days(1,31).
days(2,28).

(and so on).

The structure dateObject(M, D) describes a date. For example dateObject(6,5) would denote the 5th of June. Write the relation difference(From, To, N), where N is the number of days between starting date From and finishing date To. The From date is not included in the count. For example:

?- difference(dateobject(2,1),dateobject(2,2),N).
N = 1.

If the day or month values in a date are invalid (e.g. dateobject(4,31), dateobject(13,1) then the value of N returned should be -1. If the From date is later than To then the -1 error value should also be returned for N.

1
What does the "difference between the objects" mean? - Enigmativity
I don't understand what you mean by "make the two objects register as individual things that I can manipulate"? - Enigmativity
@Enigmativity Basically the objects represent days in a months: days(1, 31). difference(dateobject(4,30), dateobject(5,15),N). - TheLeftRight
Yes, I understand that. What does the "difference between the objects" mean? - Enigmativity
@Enigmativity the difference is the number of days between the two date objects. - TheLeftRight

1 Answers

1
votes

Here's a really bad solution - I'm sure someone smarter than me will come up with something better.

?- difference(dateobject(1,28),dateobject(6,1),N),write(N),nl.

days(1,31).
days(2,28).
days(3,31).
days(4,30).
days(5,31).
days(6,30).
days(7,31).
days(8,31).
days(9,30).
days(10,31).
days(11,30).
days(12,31).

daysbetween(M,FD,M,TD,N) :- !, N is TD - FD.
daysbetween(FM,FD,TM,TD,N) :-
    days(FM,D),
    FM2 is FM + 1,
    daysbetween(FM2,FD,TM,TD,N2),
    N is D + N2.

difference(dateobject(FM,FD),dateobject(TM,TD),N) :-
    TM >= FM,
    FD >= 1,
    days(FM,FDM),
    FD =< FDM,
    TD >= 1,
    days(TM,TDM),
    TD =< TDM,
    daysbetween(FM,FD,TM,TD,N),
    N > 0,
    !.
difference(_,_,-1).