我正在使用此代码:
item(one, 50, 40).
item(two, 80, 70).
item(three, 100, 55).
item(four, 50, 45).
maxMeanStudy:-
findall(M,item(_,M,_),L),
max_member(Max,L),
writeln(Max),!.
Run Code Online (Sandbox Code Playgroud)
要访问项目的中间值,我必须使用它来访问它item(_,M,_)
.如果只有几个条目,这是可以的.但如果有很多条目,item(_,_,_,_,_,M,_,_,_,_,_,_,_,_,_,_)
每次都很难进入.
有没有什么方法我最初声明结构item(Name, Val1, Val2)
,然后我可以访问它item(Val1)
?
只需定义一个辅助谓词,如下所示:
md_(M) :-
item(_, M, _).
Run Code Online (Sandbox Code Playgroud)
这样,您只需编写一次(可能)大量下划线.
使用SICStus Prolog 4.3.3进行示例查询:
| ?- md_(M).
M = 50 ? ;
M = 80 ? ;
M = 100 ? ;
M = 50 ? ;
no
Run Code Online (Sandbox Code Playgroud)