I have a code for calculating factorial as below :
fact(1,1). fact(X,R):- X1 is X-1, fact(X1,R1), R is R1*X.
In my mind this code shouldn't work right but it does! What is my reason? I think when we call fact(3,R), first it calculate "X1 is X1 -1". Then it goes to next rule fact(X1,R1). This will call the goal part again and the code execution will return to the goal "fact(X,R)" and this will continue until we reach to fact(1,1). It means it never goes to R is R1*X part. So, it seems I am thinking wrong.
Can anyone tell me step by step about the code execution order in this code? Thanks