【问题标题】:SWI-Prolog: ERROR: is/2: Arguments are not sufficiently instantiatedSWI-Prolog:错误:is/2:参数没有充分实例化
【发布时间】:2010-07-21 12:57:30
【问题描述】:

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

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, Count)。 我得到了一个结果。

但是当我打电话给smooth 就像?- smooth(2,100,10000,Smooths). 我收到以下错误:

ERROR: is/2: Arguments are not sufficiently instantiated

【问题讨论】:

    标签: prolog instantiation-error


    【解决方案1】:

    真的很抱歉。我整天都在试图找出问题所在,最后我发现在同一个地方我写了“FilPrimes”,在其他一些地方写了“Filprimes”。

    我真是个白痴!

    【讨论】:

    • 一般来说,您可以使用 SWI-Prolog 的图形跟踪器来跟踪执行。要查看调用 Goal 时会发生什么,请尝试 ?- gtrace, Goal。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2023-04-02
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多