【问题标题】:Verifying that the combination of two lists equals another验证两个列表的组合是否等于另一个
【发布时间】:2018-03-16 11:39:18
【问题描述】:

我正在寻找定义一个函数 plak/3 以便 plak(X,Y,Z) 在将 Y 附加到 X 时返回 true 导致 Z。所以一些示例案例:

?- plak([1,2], [2,3], [1,2,2,3]).
true
?- plak([1,2,3], [4,5], X).
X = [1,2,3,4,5].
?- plak(X,[2,3,4],[1,2,3,4]).
X = [1].

现在我的想法是这样做

plak([],[],[]).
plak([X|Xs], Y, [Z|Zs]) :-
    X==Z,
    plak(Xs, Y, Zs).
plak(X, [Y|Ys], [Z|Zs]) :-
    Y==Z, 
    plak(X,Ys,Zs).

但这不起作用。

【问题讨论】:

标签: list recursion prolog


【解决方案1】:
%if the first list is empty, add the second list to the end of the output list
concatenate([],List2,List2).
%take the first element of the first list, put it in the output list.
%Do this for all elements until the first list is empty
concatenate([H|T], List2, [H|T2]) :- concatenate(T, List2, T2).

列表实际上是一个元素链,其中每个元素都链接到下一个元素。一个链表。最后一个原子总是[],所以这允许我们通过将一个列表附加到另一个仍然打开的列表的末尾来结束其他列表。

对于 Prolog 初学者来说了解基本概念的有用链接:

【讨论】:

    猜你喜欢
    • 2017-02-28
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多