prolog - SWI-Prolog 中递增元素

标签 prolog increment

好吧,因为我今天正在练习“Prolog 逻辑”,并且我在此处的另一个线程中发现了这个未解答的问题,所以我想知道它是如何工作的。

increment every element of list by its depth, example increment([0,0,[0]],[1,1,[2]]) -> true

我几乎以为我喜欢这种语言......:)) 这样的事情可能吗?谢谢。

最佳答案

以下代码完成这项工作:

increment(X,Y):- increment(X,0,Y). 
increment([],_,[]) :- !.
increment([X|Xs],N,[Y|Ys]) :- !, N1 is N+1,
                              increment(X,N1,Y), increment(Xs,N,Ys). 
increment(X,N,Y):- Y is X+N.

只是为了测试一下

?- increment([0,0,[0]],X).
X = [1, 1, [2]].

?- increment([0,0,[0,[0,0]]],X).
X = [1, 1, [2, [3, 3]]].

关于prolog - SWI-Prolog 中递增元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10066275/

相关文章:

c - C 中的字符数组自增

prolog - SWI-Prolog(clpfd) 中变量值之间的距离最大化

java - 没有索引声明的 for 循环

prolog - 将 Prolog 代码翻译成 Lisp

prolog - 查找列表中谓词的出现次数

Java 增量和赋值运算符

Java 增量 i = i++;

当其中包含的变量增加时,Javascript 值不会改变

performance - 序言程序: clp(fd) too slow无输出

list - 将数字列表拆分为对列表列表