您的查詢不是很清楚,因爲M
和W
都是自由變量。我承擔的目標是一樣的東西:
得到一M×使得f(X,M)或f(M,X),並memoise使用動態謂詞fff
的M的值。
假設我說對了,我的解決辦法是:
:- 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.
?-
什麼FFF是什麼?你只定義謂詞'f'和'get'。 – seanmcl
'fff'是一個動態的事實包含結果 – Steve
* ...工作,但它不是最好的解決方案* - 你能定義你的意思是「最好的」? – lurker