如何合并这样的两个列表?
[a,b,c]
[1,2,3]
我希望制作此列表
[a=1,b=2,c=3].
我该怎么做? (我使用swi prolog)
Thanks for your answers. I have a question about it. I write
start:- consult('tennis.pl'),
see('tennis.pl'),
repeat,
read(A),
A=..List,
(A\=end_of_file->
(A\=end_of_file,member('attributes',List)->
delete(List,'attributes',NewList2);true),
(A\=end_of_file,member('data',List)->
delete(List,'data',NewList);true),
merge(NewList2,NewList,Try),
write(Try),nl;true),
A=end_of_file,!,
seen.
[outlook=_G40,temperature=_G49,humidity=_G58,wind=_G67,play_tennis=_G76]
[_G40=sunny,_G49=hot,_G58=high,_G67=weak,_G76=no]
[_G40=sunny,_G49=hot,_G58=high,_G67=strong,_G76=no]
[_G40=overcast,_G49=hot,_G58=high,_G67=weak,_G76=yes]
[_G40=rain,_G49=mild,_G58=high,_G67=weak,_G76=yes]
[_G40=rain,_G49=cool,_G58=normal,_G67=weak,_G76=yes]
[_G40=rain,_G49=cool,_G58=normal,_G67=strong,_G76=no]
[_G40=overcast,_G49=cool,_G58=normal,_G67=strong,_G76=yes]
[_G40=sunny,_G49=mild,_G58=high,_G67=weak,_G76=no]
[_G40=sunny,_G49=cool,_G58=normal,_G67=weak,_G76=yes]
[_G40=rain,_G49=mild,_G58=normal,_G67=weak,_G76=yes]
[_G40=sunny,_G49=mild,_G58=normal,_G67=strong,_G76=yes]
[_G40=overcast,_G49=mild,_G58=high,_G67=strong,_G76=yes]
[_G40=overcast,_G49=hot,_G58=normal,_G67=weak,_G76=yes]
[_G40=rain,_G49=mild,_G58=high,_G67=strong,_G76=no]
但我有这个结果。为什么?你有什么想法吗?
答案 0 :(得分:4)
谓词可能如下:
merge([], [], []).
merge([X|Xs], [Y|Ys], [X=Y|Zs]) :- merge(Xs, Ys, Zs).
您从空列表的基本情况开始,并通过具有相同长度的两个列表的情况进行导入。
答案 1 :(得分:2)
meta-predicate maplist/4
没问题
lambda expressions:
?- use_module(library(lambda)).
true.
?- maplist(\K^V^(K=V)^true, [a,b,c], [1,2,3], KVs).
KVs = [a=1, b=2, c=3].