Type the Prolog factorial predicate (exam A3)
Type the Prolog factorial predicate (exam A3)
Answer
fact(0, 1). fact(N, F) :- N > 0, N1 is N - 1, fact(N1, F1), F is N * F1.
Clause 1 is the base case 0! = 1, listed first so it is tried before recursing. Clause 2 guards N > 0, decrements with `is`, recurses, then multiplies on the way back up. Using `=` instead of `is` would leave F bound to the unevaluated term N*F1 instead of a number.