2

How can merge two list like this?

[a,b,c] 

[1,2,3]

and I want to make this list

[a=1,b=2,c=3]. 

How can I do this? (I use 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]

but I have this result. Why? Do you have any ideas about this?

jack
  • 21
  • 1
  • 3

2 Answers2

4

The predicate could look like:

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

You start with the base case of empty list and induct by case of two lists with the same length.

pad
  • 41,040
  • 7
  • 92
  • 166
  • Thank for your answer, I want to this. But there is a problem 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',YeniList2);true), (A\=end_of_file,member('data',List)-> delete(List,'data',YeniList);true), merge(YeniList2,YeniList,Dene), write(Dene),nl;true), A=end_of_file,!, seen. – jack May 15 '11 at 20:34
  • You should open another question and put the content of tennis.pl into that question too. – pad May 15 '11 at 22:25
2

No problem with maplist/4 together with 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].
Community
  • 1
  • 1
repeat
  • 18,496
  • 4
  • 54
  • 166