English 中文(简体)
in
原标题:Shuffle in prolog
  • 时间:2011-11-11 04:53:50
  •  标签:
  • prolog

如果L1 = [1,2,3]和L2 = [4,5,6],则L3 = [1,4,2,5,3,6]

http://code>shuffle([1,2,3],[4,5,6],[1,4,2,5,3,6]

我迄今为止已经这样做:

shuffle([X],[Y],[X,Y]).
shuffle([X|Xs],[Y|Ys],_) :- shuffle(Xs,Ys,Z), shuffle(X,Y,Z).

这是我第一次尝试撰写赞成意见书,因此我仍然试图把我头 wrap在 s子、规则和一切上。

我理解逻辑,我不敢肯定如何执行这一逻辑,这样,任何帮助都会受到极大赞赏!

Thanks!

<><>Edit>: 我已指出这一点。 如果任何人有兴趣的话,可在此找到解决办法:

shuffle([X],[Y],[X,Y]).  
shuffle([X|Xs],[Y|Ys],[Z1,Z2|Zs]) :- shuffle([X],[Y],[Z1,Z2]),shuffle(Xs,Ys,Zs).
最佳回答
shuffle([], B, B).
shuffle([H|A], B, [H|S]) :- shuffle(B, A, S).

在这类问题上,通常困难的部分不是道歉,而是确定最简单的补偿关系,解决了这一问题。

问题回答

这里简单的解决办法:

shuffle([], [], []).
shuffle([X|Xs], [Y|Ys], [X,Y|Zs]) :-
    shuffle(Xs,Ys,Zs).

将这一清单归纳为处理不平等的长度清单,是将基例改为:

shuffle(Xs, [], Xs).
shuffle([], Ys, Ys).

虽然这可能产生重复的解决办法。 如果你不把“单向”这一前提视为“单向”。

(尽管我仍然认为,你应当称之为interlace,而不是shuffle 。)





相关问题
Prolog : Learning by example

I am trying to learn a little bit about swi-prolog (beyond the basic, useless programs). Can anyone explain (perhaps in pseudocode) what this sudoku solver and the related functions are doing? If ...

Working with lists in Prolog

First off let me state that this is part of a class exercise given as homework. But, the entire assignment is much more involved than the subject of this question. So.. I am searching through two ...

SWI-Prolog conditional statements

I m trying to write a function that will test to see if the word hello is contained in a list. If it is contained, i don t want it to say "true", i want it to say : "yes, the word hello is contained ...

prolog cut off in method

I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [...

Max out of values defined by prolog clauses

I know how to iterate over lists in Prolog to find the maximum, but what if each thing is a separate clause? For example if I had a bunch of felines and their ages, how would I find the oldest kitty? ...

热门标签