Prolog 将列表初始化为零以使用 sum [英] Prolog Have a list initialized to zero to use sum

查看:37
本文介绍了Prolog 将列表初始化为零以使用 sum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个在第一次迭代时有一个空列表的谓词

I want to have a predicate that has an empty list on the first iteration so

示例(列表,结果):-

example(Lists,Result) :-

我需要结果等于 [],这是第一次使用它时,它是一个矩阵,我想不断地向它添加矩阵.

I need result to equal [], the very first time its used, Its a matrix and I want to continually add matrixes to it.

|a,b,c| |j,k,l| |s,t,u|
|d,e,f| |m,n,o| |v,w,x|
|g,h,i|.|p,q,r|,|y,z,?|

/*I want to be able to get back */
|(a+j+s),(b+k+t),(c+l+u)|
|(d+m+v),(e+n+w),(f+o+x)|
|(g+p+y),(h+q+z),(i+r+?)|

这些通常是浮点值.

我的谓词第一次需要结果是 [] 才能工作.这可能吗?我知道这是一个令人困惑的问题,如果你能理解的话,这是一个长期的问题,但任何帮助都会很棒.

I need result to be [] the first time for my predicate to work. Is this possible? I know this is a confusing question and a long shot if you will understand it but any help would be great.

推荐答案

简单真值表.当您遍历列表列表时,有 8 种可能的组合,对吗?

Simple truth table. There are 8 possible combinations as you iterate over your list of lists, correct?

尝试这样的事情:

foo( []     , []     , []     , []     ) .
foo( []     , []     , [Z|Zs] , [Z|Rs] ) :- R is 0+0+Z , foo( [] ,[] ,Zs , Rs ) .
foo( []     , [Y|Ys] , []     , [Y|Rs] ) :- R is 0+Y+0 , foo( [] ,Ys ,[] , Rs ) .
foo( []     , [Y|Ys] , [Z|Zs] , [R|Rs] ) :- R is 0+Y+Z , foo( [] ,Ys ,Zs , Rs ) .
foo( [X|Xs] , []     , []     , [R|Rs] ) :- R is X+0+0 , foo( Xs ,[] ,[] , Rs ) .
foo( [X|Xs] , []     , [Z|Xs] , [R|Rs] ) :- R is X+0+Z , foo( Xs ,[] ,Zs , Rs ) .
foo( [X|Xs] , [Y|Ys] , []     , [R|Rs] ) :- R is X+Y+0 , foo( Xs ,[] ,Zs , Rs ) .
foo( [X|Xs] , [Y|Ys] , [Z|Zs] , [R|Rs] ) :- R is X+Y+Z , foo( Xs ,Ys ,Zs , Rs ) .

这篇关于Prolog 将列表初始化为零以使用 sum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆