prolog - 在日期之间生成日期
prolog - Generating dates between dates
我需要生成两个给定日期之间的所有日期。
我的谓词 date_between(DateLow, DateHigh, X) 工作正常:
?- date_between(date(2020,2,15), date(2020,2,25), X).
X = date(2020, 2, 15) ;
X = date(2020, 2, 16) ;
....
X = date(2020, 2, 25) .
但是我觉得 predicate 太笨拙了。是否有另一种方法可以做到同样但更优雅?
我应该来回翻译日期到秒(邮票)和秒到日期吗?
我必须通过以秒为单位的转换来比较日期吗?
你可以看到我的代码:
date_between(DateLow, DateHigh, DateLow) :-
datestd_stamp(DateLow, StampLow),
datestd_stamp(DateHigh, StampHigh),
StampLow =< StampHigh.
date_between(DateLow, DateHigh, X) :-
datestd_stamp(DateLow, StampLow),
datestd_stamp(DateHigh, StampHigh),
StampLow < StampHigh,
DateLow = date(Y,M,D),
Dnxt is D + 1,
date_time_stamp(date(Y,M,Dnxt,0,0,0,0,-,-), StampNext),
stamp_date_time(StampNext, Dat, 0),
date_time_value(date, Dat, DateNxt),
date_between(DateNxt, DateHigh, X).
datestd_stamp(Data, Stamp) :-
Data = date(Y,M,D),
date_time_stamp(date(Y,M,D,0,0,0,0,-,-), StampTmp),
round(StampTmp, Stamp).
我试图改进谓词。执行时间肯定减少了。
谓词变得更简单、更快。
旧版本:
?- time((bagof(X, (date_between(date(2020,1,1), date(2100,12,31), X)), Ls))).
% 680,466 inferences, 0.149 CPU in 0.149 seconds (100% CPU, 4563901 Lips)
Ls = [date(2020, 1, 1), date(2020, 1, 2), date(2020, 1, 3),
新版本:
?- time((bagof(X, (date_between2(date(2020,1,1), date(2100,12,31), X)), Ls))).
% 207,106 inferences, 0.066 CPU in 0.066 seconds (100% CPU, 3157900 Lips)
Ls = [date(2020, 1, 1), date(2020, 1, 2), date(2020, 1, 3),
可以看到新版本的predicate:
date_between2(DateLow, DateHigh, DateLow) :-
DateLow @=< DateHigh.
date_between2(DateLow, DateHigh, X) :-
DateLow @< DateHigh,
DateLow = date(Y,M,D),
Dnxt is D + 1,
date_time_stamp(date(Y,M,Dnxt,0,0,0,0,-,-), StampNext),
stamp_date_time(StampNext, Dat, 0),
date_time_value(date, Dat, DateNxt),
date_between2(DateNxt, DateHigh, X).
我需要生成两个给定日期之间的所有日期。
我的谓词 date_between(DateLow, DateHigh, X) 工作正常:
?- date_between(date(2020,2,15), date(2020,2,25), X).
X = date(2020, 2, 15) ;
X = date(2020, 2, 16) ;
....
X = date(2020, 2, 25) .
但是我觉得 predicate 太笨拙了。是否有另一种方法可以做到同样但更优雅?
我应该来回翻译日期到秒(邮票)和秒到日期吗?
我必须通过以秒为单位的转换来比较日期吗?
你可以看到我的代码:
date_between(DateLow, DateHigh, DateLow) :-
datestd_stamp(DateLow, StampLow),
datestd_stamp(DateHigh, StampHigh),
StampLow =< StampHigh.
date_between(DateLow, DateHigh, X) :-
datestd_stamp(DateLow, StampLow),
datestd_stamp(DateHigh, StampHigh),
StampLow < StampHigh,
DateLow = date(Y,M,D),
Dnxt is D + 1,
date_time_stamp(date(Y,M,Dnxt,0,0,0,0,-,-), StampNext),
stamp_date_time(StampNext, Dat, 0),
date_time_value(date, Dat, DateNxt),
date_between(DateNxt, DateHigh, X).
datestd_stamp(Data, Stamp) :-
Data = date(Y,M,D),
date_time_stamp(date(Y,M,D,0,0,0,0,-,-), StampTmp),
round(StampTmp, Stamp).
我试图改进谓词。执行时间肯定减少了。
谓词变得更简单、更快。
旧版本:
?- time((bagof(X, (date_between(date(2020,1,1), date(2100,12,31), X)), Ls))).
% 680,466 inferences, 0.149 CPU in 0.149 seconds (100% CPU, 4563901 Lips)
Ls = [date(2020, 1, 1), date(2020, 1, 2), date(2020, 1, 3),
新版本:
?- time((bagof(X, (date_between2(date(2020,1,1), date(2100,12,31), X)), Ls))).
% 207,106 inferences, 0.066 CPU in 0.066 seconds (100% CPU, 3157900 Lips)
Ls = [date(2020, 1, 1), date(2020, 1, 2), date(2020, 1, 3),
可以看到新版本的predicate:
date_between2(DateLow, DateHigh, DateLow) :-
DateLow @=< DateHigh.
date_between2(DateLow, DateHigh, X) :-
DateLow @< DateHigh,
DateLow = date(Y,M,D),
Dnxt is D + 1,
date_time_stamp(date(Y,M,Dnxt,0,0,0,0,-,-), StampNext),
stamp_date_time(StampNext, Dat, 0),
date_time_value(date, Dat, DateNxt),
date_between2(DateNxt, DateHigh, X).