Prolog列表合并有疑问吗? [英] Prolog List Merge Question?
本文介绍了Prolog列表合并有疑问吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何合并两个这样的列表?
How can merge two list like this?
[a,b,c]
[1,2,3]
我要列出此列表
[a=1,b=2,c=3].
我该怎么做? (我使用swi prolog)
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?
推荐答案
谓词可能类似于:
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.
这篇关于Prolog列表合并有疑问吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文