Recursive multiplication in prolog

爷,独闯天下 提交于 2019-12-04 15:47:22
mat

For integer arithmetic, use constraints. All serious Prolog systems provide them. For example, for SICStus Prolog, put :- use_module(library(clpfd)) in your initialisation file to make CLP(FD) constraints available in all your programs.

Using CLP(FD) constraints and some small modifications, your initial program becomes:

int_int_prod(_, 0, 0).
int_int_prod(Num1, Num2, Result) :- 
        NewNum2 #= Num2 - 1, 
        int_int_prod(Num1, NewNum2, NewResult),
        Result #= Num1 + NewResult.

And now the point: You obviously meant your clauses to be mutually exclusive.

Insert Num2 #> 0 at an appropriate place to do that!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!