SWI-Prolog:错误:is/2:参数未充分实例化
我正在尝试创建一个程序来打印一个间隔内有多少个平滑数字。部分代码如下:
countsmooth(_, [], _, _, Count) :-
Count is 0.
countsmooth(X, [H|T], Min, Max, Count) :-
( Y is X*H,
Y =< Max
-> ( Y >= Min
-> NewX is X*H,
countsmooth(X, T, Min, Max, NCount1),
countsmooth(NewX, [H|T], Min, Max, NCount2),
Count is (1+NCount1+NCount2)
; NewX is X*H,
countsmooth(X, T, Min, Max, NCount1),
countsmooth(NewX, [H|T], Min, Max, NCount2),
Count is (NCount1+NCount2)
)
; Count is 0
).
smooth(B, I, J, Smooths) :-
( B =< 1
-> Smooths is 0
; I =:= 1
-> primes(B, FilPrimes),
countsmooth(1, Filprimes, I, J, Count),
Smooths is (1+Count)
; primes(B, FilPrimes),
countsmooth(1, Filprimes, I, J, Count),
Smooths is Count
).
还有一个谓词 primes
,它创建从 2
到 B
的所有素数。
例如,如果 B = 11
,则 FilPrimes = [2,3,5,7,11]
。
当我在 SWI-Prolog 中调用 countsmooth
时 ?- countsmooth(1, [2,3,5,7,11,13,17,19,23], 1, 100000000, 计数)
。 我得到一个结果。
但是当我像 ?- smooth(2,100,10000,Smooths) 那样调用
我收到以下错误:smooth
时。
ERROR: is/2: Arguments are not sufficiently instantiated
I'm trying to create a program that prints how many smooth numbers are within an interval. A part of the code is here:
countsmooth(_, [], _, _, Count) :-
Count is 0.
countsmooth(X, [H|T], Min, Max, Count) :-
( Y is X*H,
Y =< Max
-> ( Y >= Min
-> NewX is X*H,
countsmooth(X, T, Min, Max, NCount1),
countsmooth(NewX, [H|T], Min, Max, NCount2),
Count is (1+NCount1+NCount2)
; NewX is X*H,
countsmooth(X, T, Min, Max, NCount1),
countsmooth(NewX, [H|T], Min, Max, NCount2),
Count is (NCount1+NCount2)
)
; Count is 0
).
smooth(B, I, J, Smooths) :-
( B =< 1
-> Smooths is 0
; I =:= 1
-> primes(B, FilPrimes),
countsmooth(1, Filprimes, I, J, Count),
Smooths is (1+Count)
; primes(B, FilPrimes),
countsmooth(1, Filprimes, I, J, Count),
Smooths is Count
).
There is also a predicate primes
that creates all prime numbers from 2
to B
.
For example, if B = 11
, then FilPrimes = [2,3,5,7,11]
.
When I call countsmooth
in SWI-Prolog like?- countsmooth(1, [2,3,5,7,11,13,17,19,23], 1, 100000000, Count)
.
I get a result.
But when I call smooth
like ?- smooth(2,100,10000,Smooths).
I get the following error:
ERROR: is/2: Arguments are not sufficiently instantiated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的很抱歉。我一整天都在试图找出问题所在,最后我发现在同一个地方我写了“FilPrimes”,而在其他一些地方写了“Filprimes”。
我真是个白痴!
I'm really sorry. I have been trying all day to find out what was going wrong and finally I saw that in same places I had written "FilPrimes" and in some other places "Filprimes".
I'm such an idiot!