我正在尝试prolog。
我想执行操作以下操作:
假设,我有一个list = [7/5,6/4,5/4,6/3]
,我想在两个列表中将其分开:
列表1将包含[7,6,5,6]
列表2将包含[5,4,4,3]
您能帮我吗?
我找不到库(lambda),所以我不知道它是如何工作的 - 这个库在哪里?您为什么不说您使用的是哪种Prolog实施?可能会更容易提供帮助?
这是可以通过教科书prolog:
进行的。split_fractions([], [], []).
split_fractions([X/Y|XYs], [X|Xs], [Y|Ys]) :-
split_fractions(XYs, Xs, Ys).
这只是说如果列表为空,则两个列表为空;如果列表的头部有X/Y
,则其他列表在头部有X
和Y
,而其余列表则相同。
您可以像@lurker所说的那样使用maplist
:
split_fractions(XYs, Xs, Ys) :-
maplist(frac_num_den, XYs, Xs, Ys).
frac_num_den(X/Y, X, Y).
,如果您在此链接中找到库(lambda):
http://www.complang.tuwien.ac.at/ulrich/prolog-inedit/lambda.pl
您可以保存此文件,然后如果您在与lambda.pl
同一目录中启动swipl
,则可以写入:
?- use_module(lambda).
true.
%% !!!
?- XYs = [a/1, b/2, c/3], maplist( (X/Y)^X^Y^true, XYs, Xs, Ys).
XYs = [a/1, b/2, c/3],
Xs = [a, b, c],
Ys = [1, 2, 3].
我尚未阅读文档,但似乎我标记了空的空间!!!很重要。
从这里:
http://www.complang.tuwien.ac.at/ulrich/prolog-inedit/iso-hiord.html
似乎"文档"在该链接和lambda.pl文件中。我找不到快速解释为什么空格必须在那里的地方。
,因为我不想把任何人拒之门外,所以这是我发现的另一个lambda库,所以直接可用:
?- use_module(library(yall)).
true.
?- maplist([X/Y, X, Y]>>true, [a/1, b/2, c/3], Xs, Ys).
Xs = [a, b, c],
Ys = [1, 2, 3].
,但也许没有lambda会好多了。
[user]
.
%% `my:exampl` -- "base case" %%
my:exampl([],[],[])
:-
(
true
)
.
%% `my:exampl` -- "recursiv case" %%
my:exampl(__p__,__pq__,__q__)
:-
(
[__p_a__|__p_z__] = __p__ ,
[__p_a__/__q_a__|__pq_z__] = __pq__ ,
[__q_a__|__q_z__] = __q__ ,
my:exampl(__p_z__,__pq_z__,__q_z__) %
)
.
end_of_file
.
/* .. showing tht it works in the typical usage : */
?-
| my:exampl(__p__,[7/5,6/4,5/4,6/3],__q__)
| .
__p__ = [7, 6, 5, 6],
__q__ = [5, 4, 4, 3] ;
false.
/* .. showing tht it works in the REVERSE direction of the typical usage : */
?-
| my:exampl([7, 6, 5, 6],__pq__,[5, 4, 4, 3])
| .
__pq__ = [7/5, 6/4, 5/4, 6/3] ;
false.
/* .. showing tht it is STEADFAST in the typical usage : */
?-
my:exampl(__p__,__pq__,__q__) , __pq__ = [7/5,6/4,5/4,6/3]
.
__p__ = [7, 6, 5, 6],
__pq__ = [7/5, 6/4, 5/4, 6/3],
__q__ = [5, 4, 4, 3] ;
ERROR: Out of global stack
/* .. showing tht it FAILS in the typical usage : */
?-
| my:exampl([7, 6/*, 5, 6*/],__pq__,[5, 4, 4, 3])
| .
false.
/* .. showing tht it provides meaningful answers for the most GENERAL usage : */
?-
my:exampl(__p__,__pq__,__r__)
.
__p__ = __pq__, __pq__ = __r__, __r__ = [] ;
__p__ = [_1894],
__pq__ = [_1894/_1908],
__r__ = [_1908] ;
__p__ = [_1894, _1918],
__pq__ = [_1894/_1908, _1918/_1932],
__r__ = [_1908, _1932] ;
__p__ = [_1894, _1918, _1942],
__pq__ = [_1894/_1908, _1918/_1932, _1942/_1956],
__r__ = [_1908, _1932, _1956] ;
__p__ = [_1894, _1918, _1942, _1966],
__pq__ = [_1894/_1908, _1918/_1932, _1942/_1956, _1966/_1980],
__r__ = [_1908, _1932, _1956, _1980]
Action (h for help) ? abort
% Execution Aborted
/* .. APPENDIX : A solution with maplist does not succeed the 2nd test : */
[user]
.
my:example:maplist(Numerators, Fractions, Denominators)
:-
maplist(frac_num_den, Fractions, Numerators, Denominators)
.
frac_num_den(X/Y, X, Y).
end_of_file .
/* .. testing : */
?-
| my:exampl([7, 6, 5, 6],__pq__,[5, 4, 4, 3])
| .
__pq__ = [7/5, 6/4, 5/4, 6/3] ;
false.
?-
| my:exampl:maplist(__p__,[7/5,6/4,5/4,6/3],__q__)
| .
ERROR: apply:maplist_/3: Arguments are not sufficiently instantiated