让我们说我想构建一个包含10个元素的列表,其中每个元素可以是0或1或2.我所拥有的是两个列表List1和List2,它们是那些1和2的位置。所以如何使用这样的函数构建整个列表:
construct(List1,List2,L).
示例:
输入:
construct([1,3,5],[8],L)
输出:
L = [1,0,1,0,1,0,0,2,0,0]
答案 0 :(得分:0)
怎么样?
constructH(Top, Top, _, _, []).
constructH(Num, Top, L1, L2, [1 | Ho]) :-
Num < Top,
member(Num, L1),
Np1 is Num+1,
constructH(Np1, Top, L1, L2, Ho).
constructH(Num, Top, L1, L2, [2 | Ho]) :-
Num < Top,
member(Num, L2),
Np1 is Num+1,
constructH(Np1, Top, L1, L2, Ho).
constructH(Num, Top, L1, L2, [0 | Ho]) :-
Num < Top,
\+ member(Num, L1),
\+ member(Num, L2),
Np1 is Num+1,
constructH(Np1, Top, L1, L2, Ho).
construct(List1, List2, Lout) :-
constructH(1, 11, List1, List2, Lout).