I am having some troubles with my CS assignment. I am trying to call another rule that I created previously within a new rule that will calculate the factorial of a power function (EX. Y = (N^X)!). I think the problem with my code is that Y in exp(Y,X,N) is not carrying over when I call factorial(Y,Z), I am not entirely sure though. I have been trying to find an example of this, but I haven been able to find anything.
I am not expecting an answer since this is homework, but any help would be greatly appreciated.
Here is my code:
/* 1.2: Write recursive rules exp(Y, X, N) to compute mathematical function Y = X^N, where Y is used to hold the result, X and N are non-negative integers, and X and N cannot be 0 at the same time as 0^0 is undefined. The program must print an error message if X = N = 0. */ exp(_,0,0) :- write('0^0 is undefined'). exp(1,_,0). exp(Y,X,N) :- N > 0, !, N1 is N - 1, exp(Y1, X, N1), Y is X * Y1. /* 1.3: Write recursive rules factorial(Y,X,N) to compute Y = (X^N)! This function can be described as the factorial of exp. The rules must use the exp that you designed. */ factorial(0,X) :- X is 1. factorial(N,X) :- N> 0, N1 is N - 1, factorial(N1,X1), X is X1 * N. factorial(Y,X,N) :- exp(Y,X,N), factorial(Y,Z).
Zin the body offactorial/3. Your problem is probably in there. If you get a singleton error warning and you don't know what to do, try replacing the variable with_. If the resultant expression makes no sense, you can tell you have deeper issues.traceto debug it. My expectation is that you have variables in a funny order and you may mean something likefactorial(Y,X,N) :- exp(F, X, N), factorial(Y, F).