问题描述
append([],Xs,Xs).
append([Head|Tail],List2,[Head|Tail2]):-
append(Tail,List2,Tail2).
上部的append方法将前两个参数槽中的元素添加到第三个param变量中.
The upper append method adds elements from first two parameter slots to the third param variable.
?-append([2,1], [3,4], X).
?-X=[2,1,3,4]
我逐步看到它的方式是(这可能是错误的)->
The way i see it in steps is (which is propably wrong) ->
- append(2 | [1],[3,4],2 | X)
-
追加([1],[3,4],X)
- append(2 | [1], [3,4], 2 | X)
append([1], [3,4], X)
append(1 | [],[3,4],1 | X)
append(1 | [], [3,4], 1 | X)
就是这样.我无法确定如何将元素添加到一起,这就是我可以使用的帮助-对这种方法的工作原理进行了清晰的解释.我只是不明白[2,1]数组是如何添加到最终结果中的.
And that's it. I can't wrap my head around how it adds together the elements and that's what i could use help with - a clear explanation on how this method works. I just don't understand how the [2,1] array gets added to the final result.
推荐答案
如果在跟踪中将其重命名,则递归中的X
与原始调用中的X
不相同. >
the X
in the recursion is not the same X
as in the original call if you rename it in the trace you'll see
append(2 | [1], [3,4], 2 | X1) -- X = [2|X1]
append([1], [3,4], X1)
append(1 | [], [3,4], 1 | X2) -- X1 = [1|X2]
append ([], [3,4], [3,4]) -- X2 = [3,4]
如此X1 = [1,3,4]
和X = [2,1,3,4]
这篇关于需要帮助来理解用于追加列表元素的序言方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!