Introduction
The aim of this assignment is to make sure that you understand and are familiar with the concepts covered in the lectures and review C++ and Scheme. By the end of the assignment, you should have exercised
• Compared and contrasted the programming C++, Scheme, and Prolog.
• Prolog list operations and manipulations
• Prolog recursive rules with multiple nested calls
Reading: Textbook Chapter 5 and lecture slides.
You are expected to do the majority of the assignment outside the class meetings. Should you need assistance, or have questions about the assignment, please contact the instructor or the TA during their office hours.
You are encouraged to ask and answer questions on the course discussion board. (However, do not share your answers in the course discussion board.)
Programming Assignment (50 points)
1 Complete the code for the following problem in three programming languages C++, Scheme, and Prolog. [30 points]
𝑥 𝑖𝑓 𝑦 ≤ 0
𝑦 𝑖𝑓 𝑥 ≤ 0
𝑓𝑜𝑜(𝑥, 𝑦) = {
𝑥 + 𝑓𝑜𝑜(𝑥 − 1, 𝑦 − 2) 𝑖𝑓 𝑥 ≥ 𝑦
𝑦 + 𝑓𝑜𝑜(𝑥 − 2, 𝑦 − 3) 𝑖𝑓 𝑥 < 𝑦
Assume that both x and y are originally non-negative integers greater than 0
For example: foo(5, 6) = 6 + foo(3, 3)
foo(3, 3) = 3 + foo(2, 1)
foo(2, 1) = 2 + foo(1, -1) foo(1, -1) = 1
Thus, foo(5, 6) = 6 +3 + 2 + 1 = 12
You must use recursion and follow the Fantastic Four-Step Abstract approach to solve the problems. You must indicate these steps in the comments in your program. (Note, a Prolog rule does not return a value. You can use an additional parameter to hold the return value.)
1.1 Solve the problem in C++: Define foo(x, y) function and write a main function to call foo(x, y). Make a file question1.cpp for this solution. [10 points]
1.2 Solve the problem in Scheme: Define a procedure and call the procedure. Make a file question1.rkt for this solution. [10 points]
1.3 Solve the problem in Prolog: Define a rule foo(X, Y, Z), where Z is used for the return value. You may also use foo(X, Y) and use one of the parameters to hold the return value. Make a file question1.pl for this solution. [10 points]
2 “Arizona Best Pizzaria” made combo pizza contains exactly 45 ounces of toppings. The base has a fixed weight and is not considered in the calculation. The available toppings are listed below with their corresponding weight (in ounces). There can be multiple entries of each topping, as long as the total weight is equal to 45 ounces. [20 points]
Topping
Weight (ounces)
Pepperoni
Meatball
Basil
Olives
Chicken
5
10
7
3
8
For example, a pizza can contain 1 topping of pepperoni, 2 toppings of meatball, 2 toppings of basil, and 2 toppings of olives : 1*5 + 2*10 + 2*7 + 2*3 = 45 (ounces)
A pizza cannot contain 6 toppings of chicken : 6 * 8 = 48 45
A pizza cannot contain only 3 toppings of meatball : 3*10 = 30 < 45
2.1 Define a rule pizza(P, M, B, O, C) to find out maximum number of each topping that can be contained on a pizza, where P, M, B, O, and C are the numbers of the pepperoni, meatball, basil, olives, and chicken toppings, respectively. For instance, maximum 4 meatballs can be used on the pizza. [14]
2.2 Write a rule called q2 :- condition, to ask the following question (goal), so that the grader to type | ?- q2. To test the question. [2]
| ?- pizza(1, 2, 2, 2, 2). Put all answers of the question in a comment in the file.
2.3 Write a rule called q3 :- condition, to ask the following question (goal), so that the grader to type | ?- q3. To test the question. [2]
| ?- pizza(1, M, 1, O, C). Put all answers of the question in a comment in the file.
2.4 Write a rule called q4 :- condition, to ask the following question (goal), so that the grader to type | ?- q4. To test the question. [2]
| ?- pizza(P, M, B, O, 1). Put all answers of the question in a comment in the file.