我想删除Prolog中列表的最后n个元素,并将其放在另一个列表中,如L2。 如果我知道删除的元素的确切数量说3,这里是代码。 但我坚持变量n案例。 顺便说一下,如果列表的长度小于n,我想返回一个空字符串。 谢谢。
without_last_three([], []). without_last_three([_], []). without_last_three([_,_], []). without_last_three([_,_,_], []). without_last_three([Head|Tail], [Head|NTail]):- without_last_three(Tail, NTail).I would like to delete the last n elements of a list in Prolog and put it in another list say L2. If I knew the exact number of elements to delete say 3, here is the code. But I am stuck with the variable n case. Btw I would like to return an empty string if the length of the list is shorter than n. Thank you.
without_last_three([], []). without_last_three([_], []). without_last_three([_,_], []). without_last_three([_,_,_], []). without_last_three([Head|Tail], [Head|NTail]):- without_last_three(Tail, NTail).最满意答案
这是一般情况:
without_last_n(L, N, []) :- nonvar(L), nonvar(N), length(L, M), N > M. without_last_n(L, N, R) :- without_last_n_(L, N, R). without_last_n_(L, N, []) :- length(L, N). without_last_n_([H|T], N, [H|T1]) :- without_last_n_(T, N, T1).这满足给定的要求,并适用于各种变量实例化方案。 令解决方案复杂化的一点是要求without_last_n(L, N, []). 如果N大于L的长度,则必须成功。 如果这不是一个要求,那么更简单的without_last_n_/3就足以解决问题了。
测试...
| ?- without_last_n([1,2,3,4], 3, R). R = [1] ? ; no | ?- without_last_n([1,2,3,4], N, R). N = 4 R = [] ? ; N = 3 R = [1] ? ; N = 2 R = [1,2] ? ; N = 1 R = [1,2,3] ? ; N = 0 R = [1,2,3,4] (1 ms) yes | ?- without_last_n([1,2,3,4], N, [1,2]). N = 2 ? ; no | ?- without_last_n(L, 3, [1,2]). L = [1,2,_,_,_] ? ; no | ?- without_last_n(L, 2, R). L = [_,_] R = [] ? ; L = [A,_,_] R = [A] ? ; L = [A,B,_,_] R = [A,B] ? ; L = [A,B,C,_,_] R = [A,B,C] ? ... | ?- without_last_n(L, N, [1,2]). L = [1,2] N = 0 ? ; L = [1,2,_] N = 1 ? ; L = [1,2,_,_] N = 2 ? ; ... | ?- without_last_n(L, N, R). L = [] N = 0 R = [] ? ; L = [_] N = 1 R = [] ? ; L = [_,_] N = 2 R = [] ? ; L = [_,_,_] N = 3 R = [] ? ; ... | ?-这里可能存在的缺陷是without_last_n([1,2,3,4], N, R). 或许可以无限制地产生N = 5, R = [] , N = 6, R = []等的解决方案。但事实并非如此。 留给读者练习。 :)
Here is a general case:
without_last_n(L, N, []) :- nonvar(L), nonvar(N), length(L, M), N > M. without_last_n(L, N, R) :- without_last_n_(L, N, R). without_last_n_(L, N, []) :- length(L, N). without_last_n_([H|T], N, [H|T1]) :- without_last_n_(T, N, T1).This satisfies the given requirements, and works with a variety of variable instantiation scenarios. What complicates the solution a bit is the requirement that without_last_n(L, N, []). must succeed if N is greater than the length of L. If this was not a requirement, then the much simpler without_last_n_/3 would suffice as a solution to the problem.
Testing...
| ?- without_last_n([1,2,3,4], 3, R). R = [1] ? ; no | ?- without_last_n([1,2,3,4], N, R). N = 4 R = [] ? ; N = 3 R = [1] ? ; N = 2 R = [1,2] ? ; N = 1 R = [1,2,3] ? ; N = 0 R = [1,2,3,4] (1 ms) yes | ?- without_last_n([1,2,3,4], N, [1,2]). N = 2 ? ; no | ?- without_last_n(L, 3, [1,2]). L = [1,2,_,_,_] ? ; no | ?- without_last_n(L, 2, R). L = [_,_] R = [] ? ; L = [A,_,_] R = [A] ? ; L = [A,B,_,_] R = [A,B] ? ; L = [A,B,C,_,_] R = [A,B,C] ? ... | ?- without_last_n(L, N, [1,2]). L = [1,2] N = 0 ? ; L = [1,2,_] N = 1 ? ; L = [1,2,_,_] N = 2 ? ; ... | ?- without_last_n(L, N, R). L = [] N = 0 R = [] ? ; L = [_] N = 1 R = [] ? ; L = [_,_] N = 2 R = [] ? ; L = [_,_,_] N = 3 R = [] ? ; ... | ?-A possible flaw here is that without_last_n([1,2,3,4], N, R). perhaps could generate solutions ad infinitum of N = 5, R = [], N = 6, R = [], etc. But it doesn't. Left as an exercise for the reader. :)
更多推荐
发布评论