序言列表问题 [英] Prolog List Question

查看:130
本文介绍了序言列表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有一个谓词mergeandmap / 2应该基本上这样做:

  mergeandmap([[a1,...,an],...,[z1,...,zm]],[x1 ... xn])
%----------列表1 ------------ - 列表2--

列出2个字母(例如[a,b,c])。
列出1个包含大小(list2)元素的列表,包含1s和0s
(例如:[[0,0,1],[1,0,1],[0,1, 1]])



序列表程序应该从列表1中确定列表2中的哪些元素应该被打印,以及何时。



对于上述示例:

 ([[0,0,1],[1,0,1] [0,1,1]],[a,b,c])结果:bc abc 

另一个例子:

 ([[0,1,1],[1,0,1],[1,1, 1]],[a,b,c])结果:bc ac abc 
([[0,1],[1,0]],[a,b])结果:ba
[[0,1,1,1],[1,0,1,0],[1,1,1,0],[1,1,1,0],[A,b,C,D ])结果:bcd acd abcd a

我想出了这个想法:

$ b $($,$,$,$,$,$,$,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, c])




  1. 合并列入新列表:将所有第一个子元素在一起,所有第二个子元素,所有第三个等等... - > [ADG,BEH,CFI]

  2. Map (1 ):



    [ADG,BEH,CFI] + [abc,abc,abc]


- >大写字母的值决定了小写字母的结果。



有没有人知道如何在prolog中实现?
任何帮助真的将不胜感激!

解决方案

这是一个实现你说的话,在prolog中完成完全符合您的规格。您的合并实际上是一个称为转置(在矩阵的上下文中)的函数,这恰好在一个SWI-Prolog库中!

  1? -  [用户]。 
|:mergeandmap(Bss,Ls): - transpose(Bss,BssT),write('Result:'),maptostrings(BssT,Ls),write('\\\
'
|:maptostrings([],_)。
|:maptostrings([Bs | Bss],Ls): - 写(''),zipstringbits(Bs,Ls),maptostrings(Bss,Ls)。
|:zipstringbits([],_)。
|:zipstringbits([0 | Bs],[_ | Ls]): - zipstringbits(Bs,Ls)。
|:zipstringbits([1 | Bs],[L | Ls]): - 写(L),zipstringbits(Bs,Ls)。
|:: - use_module(library(clpfd))。
%库(错误)编译成错误0.01秒,10,056字节
%库(应用)编译成应用0.02秒,16,256字节
%库(列表)编译成列表0.00秒,13,404字节
%库(对)编译成对0.01秒,4,772字节
%库(clpfd)编译成clpfd 0.32秒,367,328字节
|:
%user:// 1编译0.44秒,369,348字节
true。

2? - mergeandmap([[0,0,1],[1,0,1],[0,1,1]],[a,b,c])。
结果:b c abc
true。

3? - mergeandmap([[0,1,1],[1,0,1],[1,1,1]],[a,b,c])。
结果:bc ac abc
true。

4? - mergeandmap([[0,1],[1,0]],[a,b])。
结果:b a
true。

5? - mergeandmap([[0,1,1,1],[1,0,1,0],[1,1,1,0],[1,1,1 ,0]],[A,b,C,D])。
结果:bcd acd abcd a
true。

6? - mergeandmap([[0,1],[1,0],[1,0]],[a,b,c])。
结果:bc a
true。

7? - 转置([[A,B,C],[D,E,F],[G,H,I]],X)。
X = [[A,D,G],[B,E,H],[C,F,I]]。

编写自己的转置函数有一点更多的参与,因为我决定自己刺。过了一段时间,我确实设法得到一个非常整洁的解决方案,这是通过设计,能够接受列表的列表,即使他们不是矩形! (尽管它仍然不能像在mergeandmap中使用的那样灵活)不幸)。以下是代码:

  1? -  [user]。 
|:转置(Rs,Cs): - emptylists(Rs),emptylists(Cs)。
|:转置([[X | R] | Rs],[[X | C] | Cs]): - 头与尾(Rs,C,NRs),头与尾(Cs,R,保护区,NCS)。
|:headsandtails(Xss,[],[]): - emptylists(Xss)。
|:headandtails([[X | Xs] | Xss],[X | Hs],[Xs | Ts]): - 头与尾(Xss,Hs,Ts)。
|:emptylists([]): - !
|:emptylists([[] | L]): - emptylists(L)。
|:
%user:// 1编译0.07秒,1,208字节
true。

2? - 转置([[1,2,3,4],[5,6,7],[8,9],[10]],结果)。
结果= [[1,5,8,10],[2,6,9],[3,7],[4]];
false。

3? - 转置([[1,2,3,4],[5,6,7],[8,9,10,11],[10]],结果)。
false。

4? - 转置([[1,2,3,4,5,7,8,3],[5,6,7],[8,9,10,11] [10]],结果)。
false。

5? - 转置([[1,2,3,4,5,7,8,3],[5,6,7],[8,9,99],[10 ]],结果)。
结果= [[1,5,8,10],[2,6,9],[3,7,99],[4],[5],[7],[8] 3]];
false。

基本上这是如何工作的,它检查转置的左上角元素和原来是相同的,并检查一行的顶行与另一列的左列匹配,反之亦然,然后重复检查其他内容,直到没有任何内容可以检查。



剪切运算符!对于emptylists谓词使得更多的空列表阻止了右边的答案,并且意味着它可以终止尝试转移一些没有一个的东西(只是不断添加它们,总是找不到答案)。



对于一个解决方案的总结,这是您需要的所有代码:

 code> mergeandmap(Bss,Ls): -  transpose(Bss,BssT),write('Result:'),maptostrings(BssT,Ls),write('\\\
')。
maptostrings([],_)。
maptostrings([Bs | Bss],Ls): - 写(''),zipstringbits(Bs,Ls),maptostrings(Bss,Ls)。
zipstringbits([],_)。
zipstringbits([0 | Bs],[_ | Ls]): - zipstringbits(Bs,Ls)。
zipstringbits([1 | Bs],[L | Ls]): - 写(L),zipstringbits(Bs,Ls)。
转置(Rs,Cs): - emptylists(Rs),emptylists(Cs)。
转置([[X | R] | Rs],[[X | C] | Cs]): - 头与尾(Rs,C,NR),头与尾(Cs,R, NCS)。
headandtails(Xss,[],[]): - emptylists(Xss)。
headandtails([[X | Xs] | Xss],[X | Hs],[Xs | Ts]): - 头与尾(Xss,Hs,Ts)。
emptylists([]): - !
emptylists([[] | L]): - emptylists(L)。


I'm trying to understand lists in prolog when I stumbpled over this problem:

there's a predicate mergeandmap/2 that should basically do this:

mergeandmap([[a1,...,an],...,[z1,...,zm]],[x1...xn])
            %----------list 1------------ -list 2--

List 2 consits of letters (for example [a,b,c]). List 1 consits of several lists with size(list2) elements containing 1s and 0s (for example: [[0,0,1],[1,0,1],[0,1,1]])

The prolog program should determine from list 1 which elements from list 2 should be printed and when.

For the above example:

([[0,0,1],[1,0,1],[0,1,1]],[a,b,c])  Result:  b c abc

Another example:

([[0,1,1],[1,0,1],[1,1,1]],[a,b,c])  Result:  bc ac abc
([[0,1],[1,0]],[a,b])  Result:  b a
([[0,1,1,1],[1,0,1,0],[1,1,1,0],[1,1,1,0]],[a,b,c,d])  Result:  bcd acd abcd a

I came up with this idea:

([[A,B,C],[D,E,F],[G,H,I]],[a,b,c])

  1. "Merge" Lists into new list: by putting all the first subelements together, all the second subelements, all third, etc, etc... -> [ADG,BEH,CFI]
  2. "Map" second list on Result from (1):

    [ADG,BEH,CFI] + [abc,abc,abc]

-> Value of uppercase letter decides wether lower case letter gets in the result.

Does anybody know how to implement this in prolog? Any help would really be appreciated!

解决方案

Well this is a an implementation of what you say, done in prolog exactly to your specifications. Your "Merge" is actually a function called transpose (in the context of matrices), and this happens to be in one of the SWI-Prolog libraries!

1 ?- [user].
|: mergeandmap(Bss,Ls) :- transpose(Bss,BssT), write('Result: '), maptostrings(BssT,Ls), write('\n').
|: maptostrings([],_).
|: maptostrings([Bs|Bss],Ls) :- write(' '), zipstringbits(Bs,Ls), maptostrings(Bss,Ls).
|: zipstringbits([],_).
|: zipstringbits([0|Bs],[_|Ls]) :- zipstringbits(Bs,Ls).
|: zipstringbits([1|Bs],[L|Ls]) :- write(L), zipstringbits(Bs,Ls).
|: :- use_module(library(clpfd)).
%    library(error) compiled into error 0.01 sec, 10,056 bytes
%   library(apply) compiled into apply 0.02 sec, 16,256 bytes
%   library(lists) compiled into lists 0.00 sec, 13,404 bytes
%   library(pairs) compiled into pairs 0.01 sec, 4,772 bytes
%  library(clpfd) compiled into clpfd 0.32 sec, 367,328 bytes
|: 
% user://1 compiled 0.44 sec, 369,348 bytes
true.

2 ?- mergeandmap([[0,0,1],[1,0,1],[0,1,1]],[a,b,c]).
Result:  b c abc
true .

3 ?- mergeandmap([[0,1,1],[1,0,1],[1,1,1]],[a,b,c]).
Result:  bc ac abc
true .

4 ?- mergeandmap([[0,1],[1,0]],[a,b]).
Result:  b a
true .

5 ?- mergeandmap([[0,1,1,1],[1,0,1,0],[1,1,1,0],[1,1,1,0]],[a,b,c,d]).
Result:  bcd acd abcd a
true .

6 ?- mergeandmap([[0,1],[1,0],[1,0]],[a,b,c]).
Result:  bc a
true .

7 ?- transpose([[A,B,C],[D,E,F],[G,H,I]],X).
X = [[A, D, G], [B, E, H], [C, F, I]].

Writing your own transpose function is a little more involved, as I decided to have a stab at it myself. After a while I did manage to get to a very neat solution, which by design, is able to accept lists of lists even when they're not rectangular! (Though it's still not as flexible as would be desired for use in mergeandmap unfortunately). Here's the code:

1 ?- [user].
|: transpose(Rs,Cs) :- emptylists(Rs),emptylists(Cs).
|: transpose([[X|R]|Rs],[[X|C]|Cs]) :- headsandtails(Rs,C,NRs), headsandtails(Cs,R,NCs), transpose(NRs,NCs).
|: headsandtails(Xss,[],[]) :- emptylists(Xss).
|: headsandtails([[X|Xs]|Xss],[X|Hs],[Xs|Ts]) :- headsandtails(Xss,Hs,Ts).
|: emptylists([]) :- !.
|: emptylists([[]|L]) :- emptylists(L).
|: 
% user://1 compiled 0.07 sec, 1,208 bytes
true.

2 ?- transpose([[1,2,3,4],[5,6,7],[8,9],[10]],Result).
Result = [[1, 5, 8, 10], [2, 6, 9], [3, 7], [4]] ;
false.

3 ?- transpose([[1,2,3,4],[5,6,7],[8,9,10,11],[10]],Result).
false.

4 ?- transpose([[1,2,3,4,5,7,8,3],[5,6,7],[8,9,10,11],[10]],Result).
false.

5 ?- transpose([[1,2,3,4,5,7,8,3],[5,6,7],[8,9,99],[10]],Result).
Result = [[1, 5, 8, 10], [2, 6, 9], [3, 7, 99], [4], [5], [7], [8], [3]] ;
false.

Basically how this works, is it checks the top left elements of the transpose and original are the same, and checks that the top row of one matches the left column of the other, and vice versa, then checks everything else iteratively until there's nothing left to check.

The cut operator ! on the emptylists predicate stops the answers growing off to the right with more empty lists, and means that it can terminate on trying to transpose something which doesn't have one (where it just keeps adding them and always fails to find an answer).

In summary for an all in one solution this is all the code you need:

mergeandmap(Bss,Ls) :- transpose(Bss,BssT), write('Result: '), maptostrings(BssT,Ls), write('\n').
maptostrings([],_).
maptostrings([Bs|Bss],Ls) :- write(' '), zipstringbits(Bs,Ls), maptostrings(Bss,Ls).
zipstringbits([],_).
zipstringbits([0|Bs],[_|Ls]) :- zipstringbits(Bs,Ls).
zipstringbits([1|Bs],[L|Ls]) :- write(L), zipstringbits(Bs,Ls).
transpose(Rs,Cs) :- emptylists(Rs),emptylists(Cs).
transpose([[X|R]|Rs],[[X|C]|Cs]) :- headsandtails(Rs,C,NRs), headsandtails(Cs,R,NCs), transpose(NRs,NCs).
headsandtails(Xss,[],[]) :- emptylists(Xss).
headsandtails([[X|Xs]|Xss],[X|Hs],[Xs|Ts]) :- headsandtails(Xss,Hs,Ts).
emptylists([]) :- !.
emptylists([[]|L]) :- emptylists(L).

这篇关于序言列表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆