在 Prolog 中分离列表 [英] Segregating Lists in Prolog

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

问题描述

我很难理解如何让我的代码显示由偶数和奇数组成的隔离列表.我什至不确定我的理解缺乏什么.我显然是这种语言的新手,必须在学校使用它.我的命令式和功能性思维不会让我知道这到底是怎么回事哈哈.

现在,不,我不是要你做我的功课!我只是请你帮我看看我缺乏理解是什么.我也查找了类似的答案,但我无法将它们转换为我应该编写此函数的方式.

请再一次,不要像我以前经常被抨击那样为此抨击我.请帮我看看我的理解缺乏什么.请不要不解释就给我答案和代码片段.

这里是:

is_even(H) :-0 是模(H,2).隔离(列表,偶数,奇数):- 隔离器(列表,偶数,奇数).分隔符([],[],[]).隔离器([H|T],E,O):-is_even(H),% 我觉得这里是我应该建立列表的地方,% 但我不知道为什么偶数或奇数没有统一.隔离器(T,E,O),write('偶数是'), write(E), nl.隔离器([H|T],E,O):-% 这里和上面一样.隔离器(T,E,O),write('奇数是'), write(O), nl.

解决方案

逻辑纯实现非常简单,感谢 :

:- use_module(library(clpfd)).list_evens_odds([],[],[]).list_evens_odds([X|Xs],[X|Es],Os) :-X mod 2 #= 0,list_evens_odds(Xs,Es,Os).list_evens_odds([X|Xs],Es,[X|Os]) :-X mod 2 #= 1,list_evens_odds(Xs,Es,Os).

我们期望成功的一些示例查询(具有有限的答案序列):

?- Xs = [1,2,3,4,5,6,7], list_evens_odds(Xs,Es,Os).Xs = [1,2,3,4,5,6,7],Es = [ 2, 4, 6 ],os = [1, 3, 5, 7] ;错误的.?- list_evens_odds(Ls,[2,4],[1,3]).Ls = [2,4,1,3] ?;Ls = [2,1,4,3]?;Ls = [2,1,3,4] ?;Ls = [1,2,4,3] ?;Ls = [1,2,3,4] ?;Ls = [1,3,2,4] ?;不

我们预计会失败的查询会怎样?

?- list_evens_odds(Ls,[2,4,5],[1,3]).不?- list_evens_odds(Ls,[2,4],[1,3,6]).不?- list_evens_odds([_,_,_],[2,4],[1,3]).不

最后,最通用的查询:

?- 断言(clpfd:full_answer).是的?- list_evens_odds(Ls,Es,Os).Ls = [], Es = [], Os = [] ?;Ls = [_A], Es = [_A], Os = [], _A mod 2#=0, _A inf..sup ?...

<小时>

编辑 2015-05-06

这是另一种使用

将元谓词 tpartition/4zeven_t/2 一起使用zodd_t/2.

bool01_t(1,true).bool01_t(0,false).zeven_t(Z,Truth) :- Z mod 2 #= 0 #<==>B,bool01_t(B,真相).%zodd_t(Z,Truth) :- Z mod 2 #= 1 #<==>B,bool01_t(B,真相).zodd_t(Z,Truth) :- Z mod 2 #= B, bool01_t(B,Truth).% 调整

zeven_t/2具体化了整数的均匀度zodd_t/2具体化了奇度.

一切就绪,让我们运行一些查询!

<上一页>?- tpartition(zeven_t,[1,2,3,4,5,6,7],Es,Os).Es = [2,4,6],Os = [1,3,5,7].?- tpartition(zodd_t ,[1,2,3,4,5,6,7],Os,Es).% 参数顺序不同Es = [2,4,6],Os = [1,3,5,7].

两者都确定性地成功.使用 list_evens_odds/3 的等效查询没有.

I am having a really hard time understanding how to get my code to show my segregated lists consisting of even and odd numbers. I am not even sure what my understanding is lacking. I am new to this language obviously and must use it for school. My imperative and functional mind won't let me know what the hell is going on with this lol.

Now, no I am not asking you to do my homework! I am simply asking you to help me see what my lack of understanding is. I have also looked up similar answers but I cannot convert them to the way I am supposed to write this function.

Please, once more, do not bash me for this like I have previously usually been bashed. Please just help me see what my understanding is lacking. Do not just give me answers and code snippets without explaining it please.

Here it is:

is_even(H) :-
   0 is mod(H, 2).

segregate(List, Even, Odd) :- segregator(List, Even, Odd).

segregator([], [], []).
segregator([H|T], E, O) :-
    is_even(H),
    % I feel here is where I am supposed to build the list, 
    % but I have no clue how since Even or Odd has not been unified.
    segregator(T, E, O),
    write('Even is '), write(E), nl.
segregator([H|T], E, O) :-
    % Same here as above.
    segregator(T, E, O),
    write('Odd is '), write(O), nl.

解决方案

A logically pure implementation is very straight-forward, thanks to :

:- use_module(library(clpfd)).

list_evens_odds([],[],[]).
list_evens_odds([X|Xs],[X|Es],Os) :-
   X mod 2 #= 0,
   list_evens_odds(Xs,Es,Os).
list_evens_odds([X|Xs],Es,[X|Os]) :-
   X mod 2 #= 1,
   list_evens_odds(Xs,Es,Os).

Some sample queries we expect to succeed (with a finite sequence of answers):

?- Xs = [1,2,3,4,5,6,7], list_evens_odds(Xs,Es,Os).
Xs = [1,2,3,4,5,6,7],
Es = [  2,  4,  6  ],
Os = [1,  3,  5,  7] ;
false.

?- list_evens_odds(Ls,[2,4],[1,3]).
Ls = [2,4,1,3] ? ;
Ls = [2,1,4,3] ? ;
Ls = [2,1,3,4] ? ;
Ls = [1,2,4,3] ? ;
Ls = [1,2,3,4] ? ;
Ls = [1,3,2,4] ? ;
no

What about queries we expect to fail?

?- list_evens_odds(Ls,[2,4,5],[1,3]).
no
?- list_evens_odds(Ls,[2,4],[1,3,6]).
no
?- list_evens_odds([_,_,_],[2,4],[1,3]).
no

At last, the most general query:

?- assert(clpfd:full_answer).
yes

?- list_evens_odds(Ls,Es,Os).
Ls = [],   Es = [],   Os = []                              ? ;
Ls = [_A], Es = [_A], Os = [], _A mod 2#=0, _A in inf..sup ? ...


Edit 2015-05-06

Here's another way to do it with !

Use the meta-predicate tpartition/4 together with zeven_t/2 or zodd_t/2.

bool01_t(1,true).
bool01_t(0,false).

zeven_t(Z,Truth) :- Z mod 2 #= 0 #<==> B, bool01_t(B,Truth).

%zodd_t(Z,Truth) :- Z mod 2 #= 1 #<==> B, bool01_t(B,Truth).
zodd_t(Z,Truth)  :- Z mod 2 #=         B, bool01_t(B,Truth). % tweaked

zeven_t/2 reifies the evenness of an integer, zodd_t/2 the oddness.

With everything in place, let's run some queries!

?- tpartition(zeven_t,[1,2,3,4,5,6,7],Es,Os).
Es = [2,4,6], Os = [1,3,5,7].
?- tpartition(zodd_t ,[1,2,3,4,5,6,7],Os,Es). % argument order differs
Es = [2,4,6], Os = [1,3,5,7].

Both succeed deterministically. The equivalent query using list_evens_odds/3 does not.

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

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