Rewrite the factorial Prolog program fact (0, 1). fact (N, R) :- N> 0, N1 is...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Answer rating: 100% (QA)
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 the full answer
Related Book For
Discrete and Combinatorial Mathematics An Applied Introduction
ISBN: 978-0201726343
5th edition
Authors: Ralph P. Grimaldi
Posted Date:
Students also viewed these accounting questions
-
Show how the recursive multiplication algorithm computes XY, where X = 1234 and Y = 4321. Include all recursive computations.
-
Exercise 13. 21 gave data on x = nerve firing frequency and y = pleasantness rating when nerves were stimulated by a light brushing stoke on the forearm. The x values and the corresponding residuals...
-
The following x and s charts based on n = 4 have shown statistical control: (a) Estimate the process parameters µ and Ï. (b) If the specifications are at 705 ± 15, and the process...
-
As purchasing agent for Laurentian Enterprises in Quebec City, you ask your buyer to provide you with a ranking of excellent, good, fair or poor for a variety of characteristics for two potential...
-
The business school at Tidewater College needs to replace its copier. The dean is considering three different Canon models the 100, 200, and 300 series. The 100 series copier is the least expensive...
-
of 12 An incompressible and frictionless fluid flows left to right at a constant volumetric flow rate through a pipe with a circular cross section of uneven diameter, as shown in the image. Order the...
-
Suppose there are \(n\) stocks. Each of them has a price that is governed by geometric Brownian motion. Each has \(v_{i}=15 \%\) and \(\sigma_{i}=40 \%\). However, these stocks are correlated, and...
-
Tim Markus, Vice President of Phone Lines, earned $92,000 in salary last year. In addition to his salary, he also received low-interest loans from his employer. Tim's interest rate on these loans is...
-
What mechanisms can be implemented to address latent conflicts within organizational structures, preempting escalation into overt confrontations that undermine productivity and cohesion ?
-
A storage tank is connected to a pond (at atmospheric pressure!) by a length of 3-in pipe and two gate valves. From previous operating experience, it has been found that when the tank is at a...
-
Explain the food production timelines and staffing rHow does purchasing of food, beverage, materials and equipment affect catering delivery for an event?oles and responsibilities of the following...
-
The basic facts are not in dispute. At the trial the government presented the testimony of four Drug Enforcement Administration (DEA) special agents. In essence their testimony established that Soto...
-
Billy Ray Nibert was convicted of first-degree murder for stabbing Eugene Snavely seventeen times with a knife. The issue on appeal is simply determining the appropriate sentence for the crime. . . ....
-
On July 12, 1987, a fire broke out at the largely abandoned Crest apartment building in Seattle. Robert Earhart was one of nearly 70 City of Seattle fire fighters who responded to the alarm. Fire...
-
Each of appellants four convictions of forcible rape involved a different victim but a similar scenario. Count 1: V.B. In late October of 2007, V.B. was working as a prostitute on Motel Drive when...
-
Whenever a man does a thoroughly stupid thing, it is always from the noblest motives . . . The Government alleges that on or about February through February 10, 2010, Defendant committed the crime of...
-
A)Company has a return on equityof ROE= 20 percent , and from earnings per share of EPS=5, it paysa 2 dividend .what is the company's sustainable growth rate?B) If the return on the equityfor a f 2...
-
Charles owns an office building and land that are used in his trade or business. The office building and land were acquired in 1978 for $800,000 and $100,000, respectively. During the current year,...
-
Four torpedoes, whose probabilities of destroying an enemy ship are 0.75, 0.80, 0.85, and 0.90, are fired at such a vessel. Assuming the torpedoes operate independently, what is the probability the...
-
A lock has n buttons labeled 1, 2, . . . , n. To open this lock we press each of the n buttons exactly once. If no two or more buttons may be pressed simultaneously, then there are n\ ways to do...
-
Consider the caterpillar in part (i) of Fig. 12.50. If we label each edge of the spine with a 1 and each of the other edges with a 0, the caterpillar can be represented by a binary string. Here that...
-
When a car accelerates gradually (no squealing tires), is the friction between tires and road kinetic or static?
-
In Figure P10.18, all surfaces experience friction. Is the magnitude of the horizontal component of the force exerted by \(A\) on \(B\) greater than, equal to, or less than the magnitude of the...
-
You keep a chalkboard eraser pressed against the chalkboard by using your finger to exert a horizontal force on the back of the eraser. (a) Which type of force (call it force A) keeps the eraser from...
Study smarter with the SolutionInn App