Introduction to Prolog programming
1. Write a query which returns all student names (first and last name), which were born in 1990.
(hint: use _ in place of those variables which are not to be shown).
2. Write a query which returns all student names which were born before 1990.
3. Write a query which returns all student names which were admitted before 2005.
4. Define a predicate under18/1 which describes those student ids which had at most 18 years when admitted.
Hint: use =<
5. Define a predicate which is satisfied if a student Ids failed to pass at least two different lectures.
6. Define a predicate which is satisfied if at least one student graduated the lecture.
Consider the following representation of natural numbers (from our last lecture):
nat(zero). nat(X) :- X = succ(Y), nat(Y).
7. Implement the predicate add/3
which adds two natural numbers.
8. Implement the predicate minus/3
which substracts one natural number from another. Substraction only needs to work as long as the result is a natural number.
Consider the following conversion predicates between naturals and Prolog integers.
toNat(0,zero). toNat(X,succ(Res)) :- X>0, Xp is X-1, toNat(Xp,Res). fromNat(zero,0). fromNat(succ(X),R) :- fromNat(X,Rp), R is Rp + 1.
9. Implement min/3
which computes the minimum of two natural numbers. Example usage:
-? toNat(7,X), toNat(4,Y), min(X,Y,R), fromNat(R,O).
10. Implement max/3
which computes the maximum of two natural numbers.
11. Implement gt/2
which is satisfied if the first natural number is strictly greater than the second.
12. Implement leq/2
which is satisfied if the first natural number is less than or equal to the second.
13. Implement div/3
which implements the div operator.
14. Implement mod/3
.
15. Implement gcd/3
which computes the greatest common divisor of two natural numbers.
Appendix
Use the following samples for testing:
%student_info(Id,First_Name,Last_Name,BirthYear) student_info(9,"John","Mike",1990). student_info(10,"Andrew","Jackson",1990). student_info(11,"Itachi","Carter",1991). student_info(12,"Luna","Ethan",1989). student_info(13,"Everly","James",1988). %student(Id,AdmissionYear) student(9,2004). student(10,2005). student(11,2006). student(12,2003). student(13,2007). %studies(Id,Lecture) studies(9,aa). studies(10,pp). studies(11,lfa). %grade(Id,Lecture,Grade) grade(10,pp,4). grade(9,pp,6). grade(10,aa,4). grade(11,lfa,10).