SWI-Prolog:错误:is/2:参数未充分实例化

发布于 2024-09-10 11:05:19 字数 1337 浏览 4 评论 0原文

我正在尝试创建一个程序来打印一个间隔内有多少个平滑数字。部分代码如下:

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,它创建从 2B 的所有素数。

例如,如果 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见终念 2024-09-17 11:05:20

我真的很抱歉。我一整天都在试图找出问题所在,最后我发现在同一个地方我写了“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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文