~/ learn/ comp-456/ cards/ Recursion & backtracking: the Prolog execution model
1 of 5

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.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/fbdd936d-d20f-4dca-b2ab-fed9af924091/flashcard utf-8 LF