您的查询不是很清楚,因为M
和W
都是自由变量。我假设目标是这样的:
得到 X 的 M 使得 f(X,M) 或 f(M,X),并使用动态谓词记忆 M 的值fff
。
假设我做对了,我的解决方案是:
:- dynamic( fff/1 ).
f(1,2).
f(1,3).
f(1,10).
f(9,1).
get( X, W ) :-
f( X, W ),
assert_fff( W ).
get( X, W ) :-
f( W, X ),
assert_fff( W ).
assert_fff( W ) :-
fff( W ),
!.
assert_fff( W ) :-
asserta( fff( W ) ).
输出:
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.4)
Copyright (c) 1990-2011 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use ?- help(Topic). or ?- apropos(Word).
?- consult('test.pl').
% test.pl compiled 0.00 sec, 2,440 bytes
true.
?- listing(fff).
:- dynamic fff/1.
true.
?- get(1, W).
W = 2 ;
W = 3 ;
W = 10 ;
W = 9.
?- listing(fff).
:- dynamic fff/1.
fff(2).
fff(3).
fff(10).
fff(9).
true.
?-