Chapter 4 - Practice

Q1: Atoms Validation

  • حدد إذا كان كل ,واحد من دول يعتبر Atom صحيح ولا لأ:
  1. 2340ieh
  2. george-smith
  3. Void
  4. _alpha
  5. x25
  6. x_y
  7. 'Sarah Jones'
  8. <---->
  9. anna
  10. alpha__beta_procedure

Q2: Structure Basics

  • اعمل structures للداتا دي باستخدام functors مناسبة:

  • Date: 1 May 2001 → use functor date

  • Person: Bob lives in London and is 48 years old → use functor person

  • Address: flat 4, street 'Home Str.', postcode eh8_9lw → use functor addr

  • وبعدين جرب الـ unification:

  • date(4, M, 2001) = date(D1, may, Y1)

  • person(Nm, london, Age) = person(bob, london, 48)

  • person(S, _, 45) = person(harry, dude, 45)

  • addr(flat(4), street('Home Str.'), postcode(eh8_9lw)) = addr(flat(Z), Yy, postcode(Xxx))


Q3: Cars Database

  • اعمل قاعدة بيانات للعربيات باستخدام structure car(Make, Age, Price):

  • Joe has a Ford car, age 3, price 5000.

  • Joe has an Opel car, age 2, price 6000.

  • Mona has a Toyota car, age 5, price 1000.

  • Mona has a Ford car, age 2, price 2000.

  • واكتب الاستعلامات:

  • What are the age and price of Mona's Ford car?

  • Who has a Ford car?

  • Display cars that have price < 5000.


Q4: Geometric Objects

  • استخدم functors point, seg, triangle عشان تمثل الأشكال الهندسية دي:

  • Point P1 with coordinates (1, 1).

  • Point P2 with coordinates (2, 3).

  • Line segment S from P1 to P2.

  • Triangle T with vertices (4,2), (6,4), (7,1).

  • وبعدين جرب الـ matching:

  • triangle = triangle

  • point(1,1) = X

  • A = point(4, Y)

  • point(2,3) = point(2, Z)


Q5: Tree Representation

  • استخدم functors عشان تمثل الـ expressions دي على شكل tree:

  • (a + b) * (c - 5) → استخدم *, +, - كـ functors

  • Parallel circuit: par(r1, r2)

  • Series circuit: seq(r1, r2)

  • par(r1, seq(par(r2, r3), r4))


Q6: Vertical & Horizontal Lines

  • اعمل rules للخطوط:

  • A line segment is vertical if its two points have the same X coordinate.

  • A line segment is horizontal if its two points have the same Y coordinate.

  • وبعدين اكتب الاستعلامات:

  • Is the segment from (1,1) to (1,2) vertical?

  • Is the segment from (1,1) to (2,Y) vertical?

  • Is the segment from (1,1) to (2,Y) horizontal? (اوجد Y)

  • Are there any vertical segments that start at (2,3)?


Q7: Employees Database

  • اعمل قاعدة بيانات للموظفين باستخدام structure employee(Name, Dept, Position, Years, Salary):

  • Ahmed works in Contracts as an Engineer for 6 years with salary 8000.

  • Mona works in Purchases as a Secretary for 3 years with salary 4000.

  • Hany works in Stores as a Head for 8 years with salary 10000.

  • Saly works in Sales as an Accountant for 4 years with salary 5000.

  • وبعدين اعمل rules:

  • Find the department a person works in → department

  • Get a person's basic salary → basic_salary

  • Get a person's real salary → real_salary : All employees with over 5 years service get a bonus of $5000.

  • واكتب الاستعلامات:

  1. What department does Ahmed work in?
  2. What is Mona's basic salary?
  3. What is Hany's real salary?
  4. Who has been working for more than 5 years?
  5. List all employees in the Sales department.

Nour Eldeen Mahmoud