2010-07-21 51 views
1

我試圖創建一個程序,打印一個區間內有多少個平滑數字。代碼的一部分是在這裏:SWI-Prolog:錯誤:是/ 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 
    ). 

還有創建所有素數從2B謂詞primes。例如,如果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 

回答

1

我真的很抱歉。我一整天都在試圖找出發生了什麼問題,最後我在相同的地方看到了我寫的「FilPrimes」,以及其他一些地方的「Filprimes」。

我真是個白癡!

+2

通常,您可以使用SWI-Prolog的圖形跟蹤器來跟蹤執行情況。要查看在調用目標時會發生什麼,請嘗試? - gtrace,Goal。 – mat 2011-02-26 23:04:24