Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time comparison in Prolog

Tags:

prolog

Say I have a structure time with the format time(hour, minute). How would I go about writing a rule to compare them? Something along the lines of compareTime(time1,time2) that returns yes if time1 is strictly before time2.

I am just starting out with Prolog after years of working with C, and the entire language is very very confusing to me.

like image 379
dc. Avatar asked Dec 21 '22 18:12

dc.


1 Answers

The standard compare/3 predicate already does what you want:

?- compare(O, time(1,1), time(1,1)).
O = (=).

?- compare(O, time(1,1), time(1,2)).
O = (<).

?- compare(O, time(1,3), time(1,2)).
O = (>).

?- compare(O, time(1,3), time(2,2)).
O = (<).

?- compare(O, time(3,2), time(2,2)).
O = (>).

so...

earlier(T1, T2) :- compare((<), T1, T2).
like image 159
salva Avatar answered Jan 19 '23 09:01

salva