Exercises 5.1
How will prolog answer the following questions?
1. X = 3*4.
2. X is 3*4.
3.4 is X.
4. X = Y.
5.3 is 1+2.
6.3 is + (.).
7.3 is x+2.
8. X is 1+2.
9.1+2 is 1+2.
Is (X, + ()).
11.3+2 = + (3,2).
12. * (7,5) = 7*5.
13. * (7, + (3,2)) = 7* (3+2).
14. * (7, (3+2)) = 7* (3+2).
15.7*3+2 = * (7, + (3,2)).
16. * (7, (3+2)) = 7* (+ (3,2)).
My answers and explanations:
1. Prolog will answer:X = 3*4, because this is a unity.
2. Prolog Answer: X = 12, because is will cause the calculation.
3. Prolog will error because the variable x appears on the right side of the is, not initialized.
4. Prolog will answer: x = y. Because this is a unity, the variables X and y will share a value.
5. Prolog will answer: TRUE.
6. Prolog will answer: TRUE.
7. Prolog will error because the variable x appears on the right side of the is, not initialized.
8. Prolog will answer: X = 3.
9. Prolog will answer: TRUE.
Prolog will answer: X = 3.
Prolog will answer: TRUE.
Prolog will answer: TRUE.
Prolog will answer: TRUE. Because two complex statements can be one.
Prolog will answer: TRUE. Because two complex statements can be one.
Prolog will answer: "False, because two complex statements cannot be one, the first complex statement is: 7*3+2, the second complex statement is: 7* (3+2).
Prolog will answer: True, because two complex statements can be one.
Exercises 5.2
1. Define a predicate INCREMENT/2, where the second parameter is 1 larger than the first parameter, for example, Increment (4, 5) is correct, increment (4,6) is wrong.
2. Define a predicate SUM/3, where the third parameter is the sum of the first parameter and the second parameter, such as sums (4, 5, 9) are correct, sum (4, 6, 12) is wrong.
My answer:
1. Increment (first, Second):-Second is first + 1.
2. SUM (ADD1, ADD2, sum):-sum is ADD1 + ADD2.
Exercises 5.3
Defines a predicate ADDONE/2, where the first parameter is a list of integers, and the second argument is the list of integers after each corresponding position element plus 1 for the first integer list, for example, query:
?-AddOne ([1, 2, 7, 2], X).
X = [2, 3, 8, 3]
My answer:
AddOne ([], []).
AddOne ([h1| T1], [h2| T2]):-H2 is H1 + 1, addone (T1, T2).
Exercises 5.4
In the previous chapters, we have discussed the ACCMAX/3 predicate, which can return the maximum value of an integer list. Please modify this verb slightly to convert to another predicate, ACCMIN/3, which can return the minimum value of an integer list.
My answer:
Accmin ([h| T], A, min):-H < A, accmin (T, H, Min).
Accmin ([h| T], A, min):-H >= A, Accmin (T, A, min).
Accmin ([], A, a).
MIN ([h| T], Min):-Accmin ([h| T], H, Min).
Exercises 5.5
In mathematics, an n-order vector is a list of n numbers. For example, [2, 5, 12] is a 3-order vector, [45, 27, 3,-4, 6] is a 5-order vector. About vectors a base operation when scalar multiplication, in this operation, the vector of the
Each element is multiplied by a number. For example, if you multiply a 3-order vector [2, 7, 4] and the number 3 for scalar multiplication, the result is a 3-order vector: [6, 21, 12].
Write a predicate SCALARMULT/3, where the first argument is an integer, the second argument is a list of integers, and the third parameter is the result of a scalar multiplication, such as:
?-Scalarmult (3, [2, 7, 4], Result).
Result = [6, 21, 12]
My answer:
Scalarmult (Num, [], []).
Scalarmult (Num, [h| T1], [h1| T2]):-H1 is H*num, Scalarmult (Num, T1, T2).
Exercises 5.6
The next question, in the vector, another base operation point multiplication (dot product). This operation will combine two vectors of the same order into a number. This is done by multiplying the elements of the two vectors at the same position and adding the results. Like what
The point multiplication [2, 5, 6] and [3, 4, 1] is: 6 + 20 + 6, or 32. Please write a DOT/3, the first parameter is a list of integers, the second parameter is a list of integers with the same length as the first argument, and the third parameter is the result of the dot multiplication. Like what:
?-Dot ([2, 5, 6], [3, 4, 1], Result).
Result = 32
My answer:
DOTACC ([], [], ACC, ACC).
DOTACC ([h1| T1], [h2| T2], ACC, Result):-
NEWACC is ACC + (H1*H2),
DOTACC (T1, T2, NEWACC, Result).
Dot (L1, L2, result):-Dotacc (L1, L2, 0, result).
Learn Prolog now translations-fifth-digital Operations-section fourth, exercises and answers