如何在列表头部之前将规则应用于列表?

时间:2016-04-10 12:13:27

标签: list prolog

我有一套这种形式的规则:

regla(o,o,o,o).

编写语句(这是家庭作业)的方式,我有一个列表作为输入,我必须在列表开始之前应用这个规则2个元素(这是可能的,因为据说之前有无限符号并且在列表开始之后,所有都是相同的。)

现在,我遇到的问题是我无法想出一种操作整个列表的方法。

也就是说,我的想法是对列表的头部进行操作,然后递归到列表的主体,最后是列表尾部的另一个操作。

类似这样的东西(遗憾的是它还没有用):

:- ensure_loaded('automaton.pl').

listsplit([H1,H2,H3|T], H1,H2,H3, T).


%regla(o,o,H1,Output).**This would be the operation to the head of the list**


%append(Output,Accumulator).%we manually add the first and last elements of the list
%cells(InList,OutList). Caso Base:
%cells( [ ], _).%perhaps cells( [ ], []). would be less dangerous


cells(Input, Result):-cellsAUX(Input, Accumulator, Result).%base case removed because examples use it in the equivalent to  the cellsAUX call

cellsAUX([], Result, Result).
cellsAUX(Input, Accumulator, Result):-**This would be the operations to the body of the list, calling itself recursively, and adding the results of each iteration to an Accumulator**
            listsplit(Input,Head1Arg1,Head2Arg1,Head3Arg1,T),
            regla(Head1Arg1,Head2Arg1,Head3Arg1,Output),
            cellsAUX([Head2Arg1,Head3Arg1|T] , [Output|Accumulator], Result).

有没有一些方法可以在没有这么多子案例的情况下做到这一点?我无法摆脱这种感觉,我觉得它比需要的更难。

作为参考,输入将类似oxoxo,正如我所说,假设前后的所有内容都是o。

提前致谢,对于形成不良的问题感到抱歉,我没有足够的词汇来打电话"这个问题是正确的方法。

0 个答案:

没有答案