Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

prolog noob question.

Status
Not open for further replies.

YFF

Technical User
Jan 24, 2013
1
US
sqr(Number) :-
power(Number,2,A),
write(A).

sqr(In,Out) :-
power(In,2,Out).

power(Base,0,1).

power(Base,Exp,Return) :-
E is Exp - 1,
power(Base,E,R),
Return is R * Base.

Why is power() not coming out of its recursion? Is my exit predicate somehow wrong? It will display the right answer(which suggests that the exit predicate is correct) but will not come back out into the repl when done. A press of the enter key will bring me back to the repl, but I don't think that is how prolog is supposed to work. Any suggestions?
 
With SWI-Prolog, type gtrace to understand what happend.
You will notice that you don't test Exp, is it always positive ? Can it be negative ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top