如何从Prolog中的列表中删除最后n个元素?(How can I remove the last n elements from a list in Prolog?)

编程入门 行业动态 更新时间:2024-10-28 18:25:21
如何从Prolog中的列表中删除最后n个元素?(How can I remove the last n elements from a list in Prolog?)

我想删除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. :)

更多推荐

本文发布于:2023-08-05 04:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   列表中   Prolog   list   elements

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!