问题描述
我很难理解如何获取代码以显示由偶数和奇数组成的隔离列表.我什至不确定我缺乏什么理解.我显然对这种语言是陌生的,必须在学校中使用它.我的命令性和实用性头脑不会让我知道这个笑声到底是怎么回事.
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.
请再一次不要为此而previously我,就像我以前通常被殴打一样.请帮助我看看我缺乏理解.不要只给我答案和代码段,而请不对其进行解释.
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.
这里是:
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.
推荐答案
由于 clpfd :
:- 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
我们期望失败的查询怎么样?
?- 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
最后,是最普遍的查询:
?- 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 ? ...
编辑2015-05-06
这是使用logical -纯度!
Edit 2015-05-06
Here's another way to do it with logical-purity!
将元谓词 tpartition/4
与zeven_t/2
或zodd_t/2
一起使用.
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
修正整数的 evenness ,zodd_t/2
修正奇数.
一切就绪,让我们运行一些查询!
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].
两个确定性成功.使用list_evens_odds/3
的等效查询不会.
Both succeed deterministically. The equivalent query using list_evens_odds/3
does not.
这篇关于在Prolog中隔离列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!