Prolog 分成两个列表问题

编程入门 行业动态 更新时间:2024-10-23 07:22:50
本文介绍了Prolog 分成两个列表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个序言作业.

我需要查看列表中的第一项,查看其后续项是否相同,直到它们不相同为止,并按第一项及其重复项将列表分开.例如,如果我的列表是 a、a、a、b、c,它将首先将其分为:a、a、a.第二个:b,c.

I need to look at the first item in a list, see if its following items are the same until they are not and separate the lists by the first item and its duplicates. e.g if my list was a,a,a,b,c it would separate it into first: a,a,a. second: b,c.

我当前的解决方案有效,只是最终匹配的项目进入第二个列表,而不是第一个.我似乎想不出办法让它出现在第一个列表中.

My current solution works except that the final matching item comes into the second list, not the first. I can't seem to think of a way to get it to appear in the first list instead.

grab([],[],[]). grab([A,A|L],[A|L2],Rest) :- grab([A|L],L2,Rest). grab([A|L],Init,[A|L2]) :- grab(L,Init,L2).

推荐答案

当前两个元素不同时,您不需要递归目标.

When the first two elements are different you do not need a recursive goal.

grab([], [], []). grab([A,A|Rest], [A|As], L2):- !, grab([A|Rest], As, L2). grab([A|Tail], [A], Tail).

更多推荐

Prolog 分成两个列表问题

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

发布评论

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

>www.elefans.com

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