2012-10-04 31 views
1

我正在學習使用SWI-Prolog的Prolog。 這裏是文件fact.pl我的實踐代碼:簡單的Prolog析因程序加載

factorial(N,F) :- N is 0, F is 1; 
       N > 0, M is N - 1, factorial(M,G), F is N*G. 

當我試圖用[fact.pl]加載該文件,解釋給我下面的錯誤:

?- [fact.pl]. 
ERROR: Syntax error: Operator expected 
ERROR: [fact 
ERROR: ** here ** 
ERROR: .pl] . 

我不知道如何發生了,我很確定我所做的是標準程序加載命令。

任何人都見過這個請幫忙,謝謝。

回答

1

要麼?- [fact].?- ['fact.pl'].應該工作

-1

當我編譯你的程序它給了我下面的答案。

階乘(5,F)。 F = 120; 錯誤。

這意味着您的程序能夠正常工作。

factorial(N,F) :- N is 0, F is 1,**!**;N > 0, M is N - 1, factorial(M,G), F is N*G.

包括切割操作(!),然後你就可以避免輸出錯誤的部分。

0

use consult(fact) or consult('fact.pl')

下面

是另一種簡單的程序

factorial(0,1). 

factorial(N,F) :- 
    N>0, 
    N1 is N-1, 
    factorial(N1,F1), 
    F is N * F1. 

the factorial of 0 is 1

the factorial of N is F if N>0 and N1 is N-1 and the factorial of N1 is F1 and F is N*F1