开发者

Prolog List Merge Question?

开发者 https://www.devze.com 2023-03-06 14:15 出处:网络
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)

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=y开发者_如何学Ces]
[_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?


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.


No problem with meta-predicate 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].
0

精彩评论

暂无评论...
验证码 换一张
取 消