Prolog 中的整数除法运算符
这段代码是我正在编写的程序的一部分,用于解决序言中的密码难题。我正在使用 CLPFD (SICStus Prolog) 解决这个问题。我在使用内置整数除法运算符“//”时遇到错误(详细信息请参见下文)。我无法解决这个问题所以我编写了自己的整数除法函数。
我想知道的是为什么“//”失败。 不存在这样的问题,
其他算术运算符如 +、- 、 / BEFORE:
solve1([],[],[H],H).
solve([H1],[H2],[H3,H4],C) :- H1 #\= 0, H2 #\= 0,
H3 #= (C+H1+H2)mod 10,
H4 #=(C+H1+H2)//10.
solve([H1|T1] , [H2|T2] , [H3|T3],C) :- H3 #= (C+H1+H2)mod 10,
C1 #=(C+H1+H2)//10 ,
solve(T1,T2,T3,C1).
这是我得到的错误
ERROR: Domain error: clpfd_expression'预期,找到
(0+_G1592+_G1586)//10' 异常:(12)抛出(错误(domain_error(clpfd_expression,(0+_G3994{0..9}+_G3991{0..9})//10),_G3976))?
之后:
integer_div(N,M) :- M #= N/10 , integer(M).
integer_div(N,M) :- N1 #= N mod 10 , N2 #= N-N1 , M #= N2/10.
solve1([],[],[H],H).
solve([H1],[H2],[H3,H4],C) :- H1 #\= 0, H2 #\= 0,
H3 #= (C+H1+H2)mod 10,
integer_div((C+H1+H2),H4).
solve([H1|T1] , [H2|T2] , [H3|T3],C) :- H3 #= (C+H1+H2)mod 10,
integer_div((C+H1+H2),C1) ,
solve(T1,T2,T3,C1).
这完美地工作了我所做的就是用我自己的整数除法版本替换“//”
This code is a part of the program that I am writing to solve a cryptarithmatic puzzle in prolog. I am solving this using CLPFD (SICStus Prolog). I am getting an error while using the inbuilt integer division operator "//" (See Below for details). I was not able to solve this So I wrote my own integer division function.
What I want to know is why "//" fails. There is no such problem with the other arithmetic operators like +, - , /
BEFORE:
solve1([],[],[H],H).
solve([H1],[H2],[H3,H4],C) :- H1 #\= 0, H2 #\= 0,
H3 #= (C+H1+H2)mod 10,
H4 #=(C+H1+H2)//10.
solve([H1|T1] , [H2|T2] , [H3|T3],C) :- H3 #= (C+H1+H2)mod 10,
C1 #=(C+H1+H2)//10 ,
solve(T1,T2,T3,C1).
This is the error I get
ERROR: Domain error: clpfd_expression' expected, found
(0+_G1592+_G1586)//10'
Exception: (12) throw(error(domain_error(clpfd_expression, (0+_G3994{0..9}+_G3991{0..9})//10), _G3976)) ?
AFTER:
integer_div(N,M) :- M #= N/10 , integer(M).
integer_div(N,M) :- N1 #= N mod 10 , N2 #= N-N1 , M #= N2/10.
solve1([],[],[H],H).
solve([H1],[H2],[H3,H4],C) :- H1 #\= 0, H2 #\= 0,
H3 #= (C+H1+H2)mod 10,
integer_div((C+H1+H2),H4).
solve([H1|T1] , [H2|T2] , [H3|T3],C) :- H3 #= (C+H1+H2)mod 10,
integer_div((C+H1+H2),C1) ,
solve(T1,T2,T3,C1).
This works perfectly All I did was replace "//" by my own version of integer division
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,CLP-FD 使用 / 进行整数除法。
According to the documentation, CLP-FD uses / for integer division.