Chapter 5 (Part 2) - Practice

Q1: Head and Tail Extraction

  • اكتب ناتج (Output) كل استعلام من الاستعلامات دي عشان تفصل الـ Head عن الـ Tail:
  1. ?- [H|T] = [a, b, c, d].
  2. ?- [H|T] = [a].
  3. ?- [H1, H2|T] = [a, b, c, d].
  4. ?- [H|T] = [].
  5. ?- [A, B, C|T] = [a, b, c, d, e].
  6. ?- [A|T] = [a].
  7. ?- [H, _|T] = [1, 2, 3].
  8. ?- [_, _|T] = [x, y, z].

Q2: List Unification

  • اكتب الناتج للاستعلامات دي (unification):
  1. ?- [X, 2, Y] = [1, 2, 3].
  2. ?- [X, 2|Y] = [1, 2, 3, 4].
  3. ?- [X, Y|Z] = [a].
  4. ?- [X|Y] = [1, 2, 3].
  5. ?- [X, Y, Z] = [a, b, c|T].
  6. ?- [a,B,c,D]=[A,b,C,d].
  7. ?- [[X,a]]=[b,Y].
  8. ?- [(a+X),(Y+b)]=[(W+c),(d+b)].
  9. ?- [[a],[B,c],[]]=[X,[b,c],Y].

Q3: Translate a List

  • استخدم الـ Facts دي اللي بترجم الأرقام لكلمات:
tran(1,one).
tran(2,two).
tran(3,three).
...
  • اكتب دالة listtran تاخد List فيها أرقام وترجع List فيها الكلمات المقابلة ليها.
  • وجربها على الاستعلام ده: ?- listtran([1, 2, 3], L).

Q4: List Membership

  • اكتب الـ Rules بتاعة الـ member وجرب الاستعلامات دي وطلع ناتجها:
  1. ?- member(c, [a, b, c, d]).
  2. ?- member(x, [a, b, c]).
  3. ?- member(X, [1, 2, 3]).

Q5: List Concatenation (Append)

  • اكتب ناتج الاستعلامات دي باستخدام دالة append:
  1. ?- append([a, b, c], [1, 2, 3], L).
  2. ?- append([a, [b, c], d], [a, [], b], L).
  3. ?- append(L1, L2, [a, b, c]).
  4. ?- append(L, _, [1, 2, 3, 4, 5, 6, 7, 8]), length(L, 5).
  5. ?- append([A], L1, [1, 2, 3, 4]).

Q6: Adding and Deleting Items

  • اكتب الـ Rule الخاصة بإضافة عنصر (Add) والـ Rule الخاصة بمسح عنصر (Delete).
  • وهات ناتج الـ Queries دي:
  1. ?- add(5, [1, 2], L).
  2. ?- del(c, [a, b, c, d], L).
  3. ?- del(a, [a, b, a, c, a], L).

Q7: Insert an item

  • استخدم الـ del بالعكس عشان تعمل Predicate اسمها insert(X, L, BiggerList) وظيفتها إنها تضيف العنصر X في أي مكان في الـ List.
  • وجرب الاستعلام: ?- insert(a, [c, d], B).

Q8: Sublist and Permutations

  • هات النواتج:
  1. ?- sublist([c, e], [a, b, c, d, e, f]).
  2. ?- sublist([a, b], [c, a, b, d]).
  3. ?- sublist(S, [a, b, d]).
  4. ?- perm([a, b, c], P).

Q9: Delete the last 3 elements

  • اكتب استعلام (Query) باستخدام الـ append بيمسح آخر 3 عناصر من أي List ويسيب الباقي في متغير جديد, وجربها على [a, b, c, d, e, f].

Q10: Mathematical Functions

  • اكتب Prolog Predicates تنفذ المعادلات دي:
  1. y = (x + 3)^2 + 3x
  2. الدالة المنطقية F = AB' + C

Q11: One_more Predicate

  • اكتب Predicate اسمها one_more(L1, L2) بتاخد List من الأرقام، وتطلع List تانية كل عنصر فيها بيزيد بواحد عن العنصر المقابل ليه في الـ List الأولى.
  • one_more([1, 2, 3], L). يخلي L = [2, 3, 4].

Q12: True/False (From Lecture)

حدّد إذا كانت الجمل دي صح ولا غلط مع تصحيح الغلط:

  1. ?-X is 9//2. Prolog will respond to this query as: X=4.5
  2. ?-X = 9/3. Prolog will respond to this query as: X=3
  3. ?-X is 9/2. Prolog will respond to this query as: X=4.5
  4. member( b, [a,b,c]).
  5. ?- append(L, [C], [1,3,4]). Prolog will respond to this query as: L=[1,3], C=4
  6. ?- L=[1,2], L1=[5|L]. Prolog will respond to this query as: L1 = [5, 1, 2].
  7. ?- L=[1,2], L1=[5|L]. Prolog will respond to this query as: L1 = [1, 2, 5].

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).

Q14: List Built-in / Arithmetic Operations

  • استنتج نواتج الدوال الجاهزة والعمليات الحسابية دي
  1. ?- length([a,b,c,d,e,[a,x],t], X).
  2. ?- sumlist([1,2,3,4], N).
  3. ?- even([a,b,c,d]).
  4. ?- reverse([1,2,3], L).
  5. ?- msort([a,c,d,b], A).

Nour Eldeen Mahmoud