将列表插入到给定位置P的另一个列表中. [英] Insert a list into another list given a position P. Prolog

查看:35
本文介绍了将列表插入到给定位置P的另一个列表中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要完成序言练习,虽然我完成了一半,但是还需要完成一些事情,这就是我寻求帮助的原因. 我需要一个小的prolog程序,给定两个列表(L1L2)和一个位置为P,将第一个列表插入第二个列表并将该列表存储在第三个列表中(L3) . insert_at(L1,L2,P,L3)

I need to finish a prolog exercise, I have half of it but I need something more to finish it, that is the reason I am asking for help. What I need is an small prolog program, given two lists (L1, L2) and one position as P, insert the first list into the second one and store that list in a third list (L3). insert_at(L1,L2,P,L3)

这里有个例子:

?- insert_at ([h1,h2], [a1,a2,a3,a4], 2,L3).
L3 = [a1,h1,h2,a2,a3,a4]

我为此编写的代码是这样的:

The code I have for this is this one:

remove_at(X,[X|Xs],1,Xs).
remove_at(X,[Y|Xs],K,[Y|Ys]) :-
    K > 1, 
    K1 is K - 1,
    remove_at(X,Xs,K1,Ys).
insert_at(X,L,K,R) :- remove_at(X,R,K,L).

这是我得到的:

?- insert_at([h1,h2],[a1,a2,a3,a4],2,L3).

L3 = [a1, [h1, h2], a2, a3, a4] % What I get

L3 = [a1, h1, h2, a2, a3, a4] % What I really want

我不知道为什么把括号放在列表中...我不想要它们,因为我已经解释了. 要完成此操作,我还需要注意更多情况:

I dont know why I get the brackets inside the list...I dont want them as I explained up. To finish it I also need to take care about more cases:

如果P高于第二个列表长度,则会在L2的末尾插入L1.

If P is higher than the second list lenght, L1 will be inserted at the end of L2.

如果我们在一个空列表中插入一个非空列表(无所谓P),我们将获得插入的列表.

If we insert a non-empty list in an empty list (no matters P), we will get the inserted list.

如果我们在非空列表中插入一个空列表(无所谓P),我们将获得非空列表.

If we insert an empty list in a non-empty list (no matters P), we will get the non-empty list.

预先感谢

推荐答案

快速解决方案:

insert_at(X, L, K, R) :-
    remove_at(X, R1, K, L),
    flatten(R1, R).

涉及重写remove_at来管理列表的解决方案:

The solution involving rewriting remove_at to manage a list:

remove_at([], Y, _, Y) :- !.       % added as a list base case
remove_at(_, [], _, []) :- !.      % added as a list base case
remove_at([X|T], [X|Xs], 1, L) :-  % handle a list [X|T] instead of just X
    remove_at(T, Xs, 1, L).
remove_at(X, [Y|Xs], K, [Y|Ys]) :- % same as before :)
    K > 1, 
    K1 is K - 1,
    remove_at(X, Xs, K1, Ys).

insert_at(X, L, K, R) :- remove_at(X, R, K, L).

第二个remove_at/4基本情况表明,如果我要从中删除的列表为空,则结果为空,并且成功.这意味着如果K大于L的长度,insert_at/4将成功,并且将返回原始列表L作为解决方案.

The second remove_at/4 base case says that if the list I want to remove from is empty, then the result is empty and it succeeds. That means insert_at/4 will succeed if K is greater than the length of L and it will return the original list, L, as the solution.

如果您希望insert_at/4K大于列表的长度时成功,并实例化R并将X附加到L(而不只是L本身),则可以将remove_at(_, [], _, []) :- !.替换为remove_at(X, X, _, []) :- !.

If you want the insert_at/4 to succeed when K is greater than the length of the list and instantiate R with X appended to L (rather than just L itself), you can replace remove_at(_, [], _, []) :- !. with remove_at(X, X, _, []) :- !.

这篇关于将列表插入到给定位置P的另一个列表中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆