Prolog列表合并问题?

时间:2011-05-15 20:16:37

标签: list merge prolog

如何合并这样的两个列表?

[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]

但我有这个结果。为什么?你有什么想法吗?

2 个答案:

答案 0 :(得分:4)

谓词可能如下:

   merge([], [], []).
   merge([X|Xs], [Y|Ys], [X=Y|Zs]) :- merge(Xs, Ys, Zs).

您从空列表的基本情况开始,并通过具有相同长度的两个列表的情况进行导入。

答案 1 :(得分:2)

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].