Chapter 5 (Part 2) - Practice
Q1: Head and Tail Extraction
- اكتب ناتج (Output) كل استعلام من الاستعلامات دي عشان تفصل الـ Head عن الـ Tail:
?- [H|T] = [a, b, c, d].?- [H|T] = [a].?- [H1, H2|T] = [a, b, c, d].?- [H|T] = [].?- [A, B, C|T] = [a, b, c, d, e].?- [A|T] = [a].?- [H, _|T] = [1, 2, 3].?- [_, _|T] = [x, y, z].
Solution (Head and Tail Extraction)
Q2: List Unification
- اكتب الناتج للاستعلامات دي (unification):
?- [X, 2, Y] = [1, 2, 3].?- [X, 2|Y] = [1, 2, 3, 4].?- [X, Y|Z] = [a].?- [X|Y] = [1, 2, 3].?- [X, Y, Z] = [a, b, c|T].?- [a,B,c,D]=[A,b,C,d].?- [[X,a]]=[b,Y].?- [(a+X),(Y+b)]=[(W+c),(d+b)].?- [[a],[B,c],[]]=[X,[b,c],Y].
Solution (List Unification)
Q3: Translate a List
- استخدم الـ Facts دي اللي بترجم الأرقام لكلمات:
tran(1,one).
tran(2,two).
tran(3,three).
...
- اكتب دالة
listtranتاخد List فيها أرقام وترجع List فيها الكلمات المقابلة ليها. - وجربها على الاستعلام ده:
?- listtran([1, 2, 3], L).
Solution (Translate a List)
Q4: List Membership
- اكتب الـ Rules بتاعة الـ
memberوجرب الاستعلامات دي وطلع ناتجها:
?- member(c, [a, b, c, d]).?- member(x, [a, b, c]).?- member(X, [1, 2, 3]).
Solution (List Membership)
Q5: List Concatenation (Append)
- اكتب ناتج الاستعلامات دي باستخدام دالة
append:
?- append([a, b, c], [1, 2, 3], L).?- append([a, [b, c], d], [a, [], b], L).?- append(L1, L2, [a, b, c]).?- append(L, _, [1, 2, 3, 4, 5, 6, 7, 8]), length(L, 5).?- append([A], L1, [1, 2, 3, 4]).
Solution (Append)
Q6: Adding and Deleting Items
- اكتب الـ Rule الخاصة بإضافة عنصر (Add) والـ Rule الخاصة بمسح عنصر (Delete).
- وهات ناتج الـ Queries دي:
?- add(5, [1, 2], L).?- del(c, [a, b, c, d], L).?- del(a, [a, b, a, c, a], L).
Solution (Adding and Deleting)
Q7: Insert an item
- استخدم الـ
delبالعكس عشان تعمل Predicate اسمهاinsert(X, L, BiggerList)وظيفتها إنها تضيف العنصرXفي أي مكان في الـ List. - وجرب الاستعلام:
?- insert(a, [c, d], B).
Solution (Insert)
Q8: Sublist and Permutations
- هات النواتج:
?- sublist([c, e], [a, b, c, d, e, f]).?- sublist([a, b], [c, a, b, d]).?- sublist(S, [a, b, d]).?- perm([a, b, c], P).
Solution (Sublist and Permutations)
Q9: Delete the last 3 elements
- اكتب استعلام (Query) باستخدام الـ
appendبيمسح آخر 3 عناصر من أي List ويسيب الباقي في متغير جديد, وجربها على[a, b, c, d, e, f].
Solution (Delete last 3 elements)
Q10: Mathematical Functions
- اكتب Prolog Predicates تنفذ المعادلات دي:
y = (x + 3)^2 + 3x- الدالة المنطقية
F = AB' + C
Solution (Mathematical Functions)
Q11: One_more Predicate
- اكتب Predicate اسمها
one_more(L1, L2)بتاخد List من الأرقام، وتطلع List تانية كل عنصر فيها بيزيد بواحد عن العنصر المقابل ليه في الـ List الأولى. one_more([1, 2, 3], L).يخليL = [2, 3, 4].
Solution (One_more Predicate)
Q12: True/False (From Lecture)
حدّد إذا كانت الجمل دي صح ولا غلط مع تصحيح الغلط:
?-X is 9//2.Prolog will respond to this query as:X=4.5?-X = 9/3.Prolog will respond to this query as:X=3?-X is 9/2.Prolog will respond to this query as:X=4.5member( b, [a,b,c]).?- append(L, [C], [1,3,4]).Prolog will respond to this query as:L=[1,3], C=4?- L=[1,2], L1=[5|L].Prolog will respond to this query as:L1 = [5, 1, 2].?- L=[1,2], L1=[5|L].Prolog will respond to this query as:L1 = [1, 2, 5].
Solution (True/False)
Q13: MCQs (From Lecture)
1. ?- L=[a, b, c, d, e], append([ _ ], [A1,A2| _ ], L).
- a) A1 = c, A2 = d.
- b) A1 = b, A2 = c.
- c) A1 = d, A2 = e.
- d) A1 = a, A2 = b.
2. Write a goal to delete the first two elements from a list L producing list L2.
- a)
?-append( _ , _ , L2, L) - b)
?-append([ _ , _ ], L2, L) - c)
?-append(L, [ _ , _ ], L2) - d)
?-append([ _ , _ ], L, L2)
3. To find the output of the function X = A^2 + AB. In prolog, we can write:
- a)
f(A,B,X):- X = A^2+A*B. - b)
f(A,B,X):- X = A*A+A*B. - c)
f(A,B,X):- X is A^2+AB. - d)
f(A,B,X):- X is A^2+A*B.
4. Write a rule to find the last 2 elements of the list.
- a)
last2(L,A,B):- append(_,A,B,L). - b)
last2(L,A,B):- append(_,(A1,A2),L). - c)
last2(L,A,B):- append(L,_,[A,B]). - d)
last2(L,A,B):- append(_,[A,B],L).
5. Write a goal to find the last 2 elements of the list.
- a)
?-append( L, A, B) - b)
?-append(_,[A, B],L) - c)
?-append(_, (A,B], L) - d)
?-append([A,B], _, L)
6. To find the output of the Boolean function F = B' + A. In prolog, we can write:
- a)
fun(A, B, F):- not(B, X ), and(X, A, F). - b)
fun(A, B, F):- or(B', A ). - c)
fun(A, B, F):- not(B, X ), or(X, A, F). - d)
fun(A, B, F):- not(B ), or(A, B, F).
Solution (MCQs)
Q14: List Built-in / Arithmetic Operations
- استنتج نواتج الدوال الجاهزة والعمليات الحسابية دي
?- length([a,b,c,d,e,[a,x],t], X).?- sumlist([1,2,3,4], N).?- even([a,b,c,d]).?- reverse([1,2,3], L).?- msort([a,c,d,b], A).
Solution (Operations)