gpt4 book ai didi

list - Prolog中列表的条件解析

转载 作者:行者123 更新时间:2023-12-02 03:19:10 25 4
gpt4 key购买 nike

考虑这三个列表:

["cal"]          %Execute Condition 1 if list 1 is passed 
["cal",2,1947] %Execute Condition 2 if List 2 is passed
["Hello","World","Random","List"] %Do not execute anything

如果列表 1 通过,则预期的输出

calendar(1, 2016)

如果列表 2 通过,则预期输出

calendar(2, 1947)

到目前为止我的代码

% Execute this predicate for condition 2
cal(X,Y,Z1,Z2) :-
Z1 is X,
Z2 is Y.

% Executed this predicate for condition 1
cal(Z1,Z2) :-
monthyear(Z1,Z2).

monthyear(M,Y) :-
get_time(Stamp),
stamp_date_time(Stamp, DateTime, local),
date_time_value(month, DateTime, M),
date_time_value(year, DateTime, Y).

如何完成我的代码以支持所需的功能?我尝试过的:

parentcal(X,Y) :-
X = [H1|T1],
H1 is "cal",
T1 = [H2|T2],
T3 = [H3|T3],
cal(H2, H3, M, Y),
write("calendar("),
write(M),
write(" "),
write(Y),
write(")").

parentcal(X,Y) :-
X = [H1|T1],
H1 is "cal",
T1 = [H2|T2],
H2 is null,
T3 = [H3|T3],
H3 is null,
cal(M, Y),
write("calendar("),
write(M),
write(" "),
write(Y),
write(")").

最佳答案

也许我遗漏了什么,但它似乎不应该比这样的东西复杂得多:

do_something( [cal] , R ) :-
get_time(TS) ,
stamp_date_time(TS,DT,local) ,
date_time_value(month,M) ,
date_time_value(year,Y) ,
do_something( [cal,M,Y] , R )
.
do_something( [cal,M,Y] , calendar( M , Y ) ) :-
int(M) ,
between(1,12,M) ,
int(Y) ,
Y > 0
.

关于list - Prolog中列表的条件解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34623549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com