Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function Prolog幂函数_Function_Prolog_Exponentiation - Fatal编程技术网

Function Prolog幂函数

Function Prolog幂函数,function,prolog,exponentiation,Function,Prolog,Exponentiation,我不熟悉Prolog,虽然我能理解代码,但我发现很难创建程序。我试图创建一个函数,它接受一个整数并返回2^(整数)示例pow(4)返回16(2^4)。我还需要它在一个循环中保持输入,直到用户输入负整数,然后它退出 在这个例子中,C是计数器,X是用户输入,试图在输出中包含变量,但无法考虑如何集成它 pow(0):- 0. pow(1):- 2. pow(X):- X > 1, X is X-1, power(X), C is X-1, pow(X1),

我不熟悉Prolog,虽然我能理解代码,但我发现很难创建程序。我试图创建一个函数,它接受一个整数并返回2^(整数)示例pow(4)返回16(2^4)。我还需要它在一个循环中保持输入,直到用户输入负整数,然后它退出

在这个例子中,C是计数器,X是用户输入,试图在输出中包含变量,但无法考虑如何集成它

pow(0):- 0.
pow(1):- 2.
pow(X):-
   X > 1, 
   X is X-1,
   power(X),
   C is X-1,
   pow(X1),
   X is 2*2.
pow(X):- X<0, C is 0.
pow(C).
pow(0):-0。
战俘(1):-2。
战俘(X):-
X>1,
X是X-1,
功率(X),
C是X-1,
功率(X1),
X是2*2。

pow(X):-X在尝试编程之前,您确实需要阅读一些关于Prolog的内容。例如,浏览一下

Prolog没有“函数”:有谓词。所有输入和输出都通过谓词参数,谓词本身不返回任何内容

所以
pow(0):-0。
pow(1):-2。
没有任何意义。您需要的是
pow(0,0)。
pow(1,2)。
:第一个参数是输入,第二个参数是输出

X是X-1
也没有意义:在Prolog中,变量就像代数变量一样,X在整个方程组中表示相同的值。变量基本上只写一次,在这种和类似的情况下,您必须引入新变量:
X1是X-1


希望这些信息足够让您开始学习。

在尝试使用Prolog编程之前,您确实需要阅读一些有关Prolog的内容。例如,浏览一下

Prolog没有“函数”:有谓词。所有输入和输出都通过谓词参数,谓词本身不返回任何内容

所以
pow(0):-0。
pow(1):-2。
没有任何意义。您需要的是
pow(0,0)。
pow(1,2)。
:第一个参数是输入,第二个参数是输出

X是X-1
也没有意义:在Prolog中,变量就像代数变量一样,X在整个方程组中表示相同的值。变量基本上只写一次,在这种和类似的情况下,您必须引入新变量:
X1是X-1


希望这些信息足够让您开始学习。

在尝试使用Prolog编程之前,您确实需要阅读一些有关Prolog的内容。例如,浏览一下

Prolog没有“函数”:有谓词。所有输入和输出都通过谓词参数,谓词本身不返回任何内容

所以
pow(0):-0。
pow(1):-2。
没有任何意义。您需要的是
pow(0,0)。
pow(1,2)。
:第一个参数是输入,第二个参数是输出

X是X-1
也没有意义:在Prolog中,变量就像代数变量一样,X在整个方程组中表示相同的值。变量基本上只写一次,在这种和类似的情况下,您必须引入新变量:
X1是X-1


希望这些信息足够让您开始学习。

在尝试使用Prolog编程之前,您确实需要阅读一些有关Prolog的内容。例如,浏览一下

Prolog没有“函数”:有谓词。所有输入和输出都通过谓词参数,谓词本身不返回任何内容

所以
pow(0):-0。
pow(1):-2。
没有任何意义。您需要的是
pow(0,0)。
pow(1,2)。
:第一个参数是输入,第二个参数是输出

X是X-1
也没有意义:在Prolog中,变量就像代数变量一样,X在整个方程组中表示相同的值。变量基本上只写一次,在这种和类似的情况下,您必须引入新变量:
X1是X-1


希望这些信息足够让您开始学习。

简单的递归解决方案:

pow2(0,1) .     % base case: any number raised to the 0 power is 1, by definition
pow2(N,M) :-    % a positive integral power of 2 is computed thus:
  integer(N) ,  % - verify than N is an inetger
  N > 0 ,       % - verify that N is positive
  N1 is N-1 ,   % - decrement N (towards zero)
  pow2(N1,M1) , % - recurse down (when we hit zero, we start popping the stack)
  M is M1*2     % - multiply by 2
  .             %
pow2(N,M) :-    % negative integral powers of 2 are computed the same way:
  integer(N) ,  % - verify than N is an integer
  N < 0 ,       % - verify than N is negative
  N1 is N+1 ,   % - increment N (towards zero).
  pow2(N1,M) ,  % - recurse down (we we hit zero, we start popping the stack)
  M is M / 2.0  % - divide by 2.
  .             % Easy!
pow2(0,1)。%基本情况:根据定义,任何提升到0次方的数字都是1
pow2(N,M):-%因此计算出2的正整数幂:
整数(N),%-验证N是否为inetger
N>0,%-验证N是否为正
N1为N-1,%-减量N(接近零)
pow2(N1,M1),%-向下递归(当我们达到零时,我们开始弹出堆栈)
M是M1*2%-乘以2
.             %
pow2(N,M):-%2的负整数幂的计算方法相同:
整数(N),%-验证N是否为整数
N<0,%-验证N是否为负值
N1为N+1,%-增量N(接近零)。
pow2(N1,M),%-向下递归(我们达到零,我们开始弹出堆栈)
M为M/2.0%-除以2。
.             % 容易的!
但是,当递归级别足够高时(忽略算术溢出问题),上述操作将使堆栈溢出。所以

尾部递归解决方案优化为迭代:

pow2(N,M) :-      %
  integer(N) ,    % validate that N is an integer
  pow2(N,1,M)     % invoke the worker predicate, seeding the accumulator with 1
  .               %

pow2(0,M,M) .     % when we hit zero, we're done
pow2(N,T,M) :-    % otherwise...
  N > 0 ,         % - if N is positive,
  N1 is N-1 ,     % - decrement N
  T1 is T*2 ,     % - increment the accumulator
  pow2(N1,T1,M)   % - recurse down
  .               %
pow2(N,T,M) :-    % otherwise...
  N < 0 ,         % - if N is negative,
  N1 is N+1 ,     % - increment N
  T1 is T / 2.0 , % - increment the accumulator
  pow2(N1,T1,M)   % - recurse down
  .               %
pow2(N,M):-%
整数(N),%验证N是否为整数
pow2(N,1,M)%调用worker谓词,在累加器中植入1
.               %
功率2(0,M,M)。%当我们达到零,我们就完了
pow2(N,T,M):-%否则。。。
N>0,%-如果N为正,
N1为N-1,%-减量N
T1为T*2,%-累加器增量
pow2(N1,T1,M)%-向下递归
.               %
pow2(N,T,M):-%否则。。。
N<0,%-如果N为负,
N1为N+1,%-增量N
T1为T/2.0,%-累加器增量
pow2(N1,T1,M)%-向下递归
.               %

这种[幼稚的]递归解决方案:

pow2(0,1) .     % base case: any number raised to the 0 power is 1, by definition
pow2(N,M) :-    % a positive integral power of 2 is computed thus:
  integer(N) ,  % - verify than N is an inetger
  N > 0 ,       % - verify that N is positive
  N1 is N-1 ,   % - decrement N (towards zero)
  pow2(N1,M1) , % - recurse down (when we hit zero, we start popping the stack)
  M is M1*2     % - multiply by 2
  .             %
pow2(N,M) :-    % negative integral powers of 2 are computed the same way:
  integer(N) ,  % - verify than N is an integer
  N < 0 ,       % - verify than N is negative
  N1 is N+1 ,   % - increment N (towards zero).
  pow2(N1,M) ,  % - recurse down (we we hit zero, we start popping the stack)
  M is M / 2.0  % - divide by 2.
  .             % Easy!
pow2(0,1)。%基本情况:根据定义,任何提升到0次方的数字都是1
pow2(N,M):-%因此计算出2的正整数幂:
整数(N),%-验证N是否为inetger
N>0,%-验证N是否为正
N1为N-1,%-减量N(接近零)
pow2(N1,M1),%-递归d