更新时间:2023-11-18 21:51:10
不确定你的例子是否正确,但我明白了.
Not sure your example is correct, but I get the idea.
如果使用 SWI-PROLOG,您可以使用 CLPFD 模块,例如所以:
If using SWI-PROLOG, you can use the CLPFD module, like so:
:- use_module(library(clpfd)).
允许你使用 transpose/2
谓词,像这样:
Allowing you to use the transpose/2
predicate, like this:
1 ?- transpose([[1,2,3],[4,5,6],[6,7,8]], X).
X = [[1, 4, 6], [2, 5, 7], [3, 6, 8]].
否则(如果没有 SWI-PROLOG),您可以简单地使用这个实现(恰好是 SWI 的 clpfd 中的旧实现):
Otherwise (if no SWI-PROLOG), you could simply use this implementation (which happened to be an old one in SWI's clpfd):
transpose([], []).
transpose([F|Fs], Ts) :-
transpose(F, [F|Fs], Ts).
transpose([], _, []).
transpose([_|Rs], Ms, [Ts|Tss]) :-
lists_firsts_rests(Ms, Ts, Ms1),
transpose(Rs, Ms1, Tss).
lists_firsts_rests([], [], []).
lists_firsts_rests([[F|Os]|Rest], [F|Fs], [Os|Oss]) :-
lists_firsts_rests(Rest, Fs, Oss).
有关使用 foldl 和 maplist 内置插件的更新版本,请参阅 clpfd.pl.
For an updated version which uses foldl and maplist built-ins, see clpfd.pl.