Question: Rewrite the factorial Prolog program fact (0, 1). fact (N, R) :- N> 0, N1 is N-1, fact (N1, R1), R is N. R1.

Rewrite the factorial Prolog program fact (0, 1). fact (N, R) :- 

Rewrite the factorial Prolog program fact (0, 1). fact (N, R) :- N> 0, N1 is N-1, fact (N1, R1), R is N. R1. to make it tail recursive (see Exercise 4.17). Draw search trees of subgoals and explain Prolog's responses based on the trees for the following goals (see Figure 4.1): (a) god (15, 10, x). (b) append(X, Y, [1, 2]). = Rewrite the Prolog clauses for Euclid's algorithm (gcd) in Figure 4.1 to use the cut instead of the test not(V 0). How much does this improve the efficiency of the program? Redraw the search tree of Exercise 4.21(a) to show how the cut prunes the tree.

Step by Step Solution

3.42 Rating (149 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres the tailrecursive version of the factorial program factN R facthelperN 1 R facthelper0 Acc Acc facthelperN Acc R N 0 Acc1 is Acc N N1 is N 1 facthelperN1 Acc1 R In this version the predicate fac... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!