PPT Slide
A RECURSIVE PROLOG PROGRAM FOR FACTORIAL
factorial( 0 , 1 ) .
factorial( N , ANS ) : -
N1 is N - 1 ,
factorial( N1 , ANS2 ) ,
ANS is N * ANS2 .
| ? - factorial( 3 , X ) .
N11 2
factorial( 2 , ANS21 )
N12 1
factorial( 1 , ANS22 )
N13 0
factorial( 0 , ANS23 )
factorial( 0 , 1 )
ANS3 1 * 1 = 1
ANS2 2 * 1 = 2
ANS1 3 * 2 = 6
X = 6
What happens if you now type ‘; ’ ?
bound to ANS1
bound to ANS2
bound to ANS3
P.26
Previous slide
Next slide
Back to first slide
View graphic version