0

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). 
3
  • 1
    Notice you get a singleton variable warning about Z in the body of factorial/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. Commented Dec 5, 2015 at 23:33
  • @DanielLyons Ok so I fixed what you suggested now the problem seems to be that the program performs X^N however it does not proceed to compute the factorial of it. Commented Dec 6, 2015 at 1:35
  • Try using trace to debug it. My expectation is that you have variables in a funny order and you may mean something like factorial(Y,X,N) :- exp(F, X, N), factorial(Y, F). Commented Dec 6, 2015 at 2:04

1 Answer 1

1

The Z variable mentioned in factorial/3 (mentioned only once; so-called 'singleton variable', cannot ever get unified with anything ...).

Noticed comments under question, short-circuiting it to _ won't work, you have to unify it with a sensible value (what do you want to compute / link head of the clause with exp and factorial through parameters => introduce some parameter "in the middle"/not mentioned in the head).

Edit: I'll rename your variables for you maybe you'll se more clearly what you did:

factorial(Y,X,Result) :- exp(Y,X,Result), factorial(Y,UnusedResult). 

now you should see what your factorial/3 really computes, and how to fix it.

Sign up to request clarification or add additional context in comments.

3 Comments

Read both sentences: I'm saying if you replace a singleton with _ and the result makes no sense (it wouldn't in this case) then there is a defect in your reasoning. Some beginners struggle with singleton variable warnings and this helps show them the problem.
What you are suggesting makes no sense to me. Singleton warning is not fatal, the meaning of the code is the same either way. I'd suggest exactly the opposite, to think hard about what's the meaning of the "parameter position" where the singleton occurs. OP just haphazardly wrote down some variable names without even thinking what each represents ...
We agree about everything except where the root of the error is. The real error is in the mind of the student. They come from C and they do not understand how serious the singleton warning is. My suggestion doesn't change the meaning to Prolog--but making the change often helps the student to see how their mental model of what Prolog is doing is defective. We have the same goal: make the OP think hard about the meaning in that position. Keep in mind 90% of Prolog questions come from experienced procedural programmers just starting with Prolog. Their intuition is often the deeper problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.