-1

I am currently trying to create a program that takes two inputs a base and an exponent, so basically im going to ask for those two things for example if the user of Prolog inputs base 2 and exponent 3, I want it to return 8.., Here is what I got so far, and doesnt work:

base:- write('Input the base: '),read(X),expo(X).
exponent:- write('Input the exponent '),read(Y),expo(Y).


expo(X,Y):- A is Y*Y,B is A*X,write(B).

HELP PLEASE

1
  • 4
    Doesn't work is nearly useless as debugging advice goes; what doesn't work? What output do you get? What output do you expect? Why does your expo(A,B):- appear to define a rule that takes two parameters, but your base:- and exponent:- appear to call it with just one parameter?
    – sarnold
    Mar 2, 2012 at 1:36

2 Answers 2

5
  • You've defined expo/2 (i.e. a predicate expo with two arguments), but you're calling expo/1.
  • The way you've split up your read/1 calls into different predicates means X and Y are in different scopes; you'll never be able to call expo(X,Y) unless you put read(X) and read(Y) within the same rule.
  • Your exponentiation definition is wrong. X^Y is not Y*Y*X. If you're required to implement this yourself, you'll need a recursive predicate to do it. If not, use the built-in exponentiation operator, X**Y.
1

See this question to see how to implement pow correctly. For the input part, you might want to consider not to bother implementing it until your pow/ 3 predicate works. To test this predicate, you can just use ?- pow(2, 3, R). in the meantime.

And Prolog should answer with R = 8..

And as said in a previous answer, the input part is wrong. But this previous answer already gave sufficient leads about how to better the situation so I'll leave it at that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.