简单的Prolog阶乘程序加载

时间:2012-10-04 13:10:08

标签: prolog factorial

我正在使用SWI-Prolog学习Prolog。 这是我在fact.pl文件中的练习代码:

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] .

我不确定这是怎么发生的,我很确定我所做的是标准程序加载命令。

任何人都见过这个请帮助,谢谢。

3 个答案:

答案 0 :(得分:1)

?- [fact].?- ['fact.pl'].应该有效

答案 1 :(得分:0)

  

使用consult(fact)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

答案 2 :(得分:-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.

包括剪切算子(!)。然后你可以避免输出中的错误部分。