Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
pp:l10 [2020/04/27 11:33]
pdmatei created
pp:l10 [2021/05/25 15:44] (current)
roxana_elena.stiuca [Introduction to Prolog programming]
Line 1: Line 1:
 ====== Introduction to Prolog programming ====== ====== 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): Consider the following representation of natural numbers (from our last lecture):
Line 7: Line 23:
 </​code>​ </​code>​
  
-1. Implement the predicate ''​add/​3''​ which adds two natural numbers. +7. Implement the predicate ''​add/​3''​ which adds two natural numbers. 
-2. 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.+ 
 +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. Consider the following conversion predicates between naturals and Prolog integers.
Line 19: Line 36:
 </​code>​ </​code>​
  
-3. Implement ''​min/​3''​ which computes the minimum of two natural numbers. Example usage:+9. Implement ''​min/​3''​ which computes the minimum of two natural numbers. Example usage:
 <code prolog> <code prolog>
 -? toNat(7,X), toNat(4,Y), min(X,Y,R), fromNat(R,​O). -? toNat(7,X), toNat(4,Y), min(X,Y,R), fromNat(R,​O).
 </​code>​ </​code>​
  
-4. Implement ''​max/​3''​ which computes the maximum ​of two natural numbers. +10. Implement ''​max/​3''​ which computes the maximum of two natural numbers.
-5. Implement ''​gt/​2''​ which is satisfied if the first natural number is strictly greater than the second. +
-6. Implement ''​leq/​2''​ which is satisfied if the first natural number is less than or equal to the second. +
-7. Implement ''​div/​3''​ which implements the **div** operator. +
-8. Implement ''​mod/​3''​. +
-9. Implement ''​gcd/​3''​ which computes the greatest common divisor ​of two natural numbers.+
  
-Consider the following representation of lists, expressed by the predicate ​''​isList'' ​shown below. +11. Implement ​''​gt/2'' ​which is satisfied if the first natural number is strictly greater than the second.
-<code prolog>​ +
-isList(void). +
-isList(cons(_,​T)) :- isList(T). +
-</​code>​+
  
-10. Implement ''​head/​2''​ and ''​tail/​2''​. +12. Implement ''​leq/​2''​ which is satisfied if the first natural number is less than or equal to the second.
-11. Implement ''​size/​2''​ which determines ​the length of a list as a Prolog integer. +
-12. Implement ''​concat/​3''​ which concatenates two lists. +
-13. Implement ''​reverse/​2''​ which reverses a list. Use accumulators instead of concatenation and an auxiliary predicate.+
  
-Consider ​the following conversion functions from the above list representation to Prolog lists.+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: 
 +
 <code prolog> <code prolog>
-fromList(void,[]). +%student_info(Id,First_Name,​Last_Name,​BirthYear) 
-fromList(cons(H,T),[H|R]:- fromList(T,R). +student_info(9,"​John","​Mike",​1990). 
-</​code>​+student_info(10,"​Andrew","​Jackson",​1990)
 +student_info(11,"​Itachi","​Carter"​,1991)
 +student_info(12,"​Luna","​Ethan",​1989). 
 +student_info(13,"​Everly","​James",​1988).
  
-14. Write the predicate ''​toList/​2''​ which converts a Prolog list to our list representation. +%student(Id,​AdmissionYear) 
-15. Implement ''​kelem/​3''​ which determines the k-th element of a Prolog list+student(9,​2004)
-16. Implement ''​rem/​2''​ which removes consecutive duplicates from a Prolog list+student(10,​2005)
-<​code>​ +student(11,2006). 
--? rem([1,1,​1,​2,​2,​3,​4,​4],​R). +student(12,2003)
-R = [1,2,3,4]+student(13,​2007)
-</​code>​ + 
-17. Implement ''​flatten/​2''​ which flattens nested Prolog lists. Do not use auxiliary predicates+%studies(Id,Lecture
-<​code>​ +studies(9,aa)
--? flatten([1,2,​[3,​4,​[5]],​ [[6],[]], [7]], R). +studies(10,pp). 
- R = [1,2,​3,​4,​5,​6,​7] +studies(11,lfa)
-</​code>​ + 
-18. Implement ''​pack/​2''​ which groups consecutive duplicates into sublists+%grade(Id,Lecture,Grade) 
-<​code>​ +grade(10,pp,4). 
-?- pack([1,1,​1,​2,​3,​3,​1,​1,​4,​5,​5,​5,​5],​R). +grade(9,pp,6)
- R = [[1,1,​1],​[2],​[3,​3],​[1,​1],​[4],​[5,​5,​5,​5]] +grade(10,aa,4). 
-</​code>​ +grade(11,lfa,10).
-19. Implement ''​slice/​4''​ which returns the elements of a list between two positions+
-<​code>​ +
-?- slice([1,2,​3,​4,​5,​6,​7,​8,​9,10],3,7,R). +
-R = [4,5,6,7,8] +
-</​code>​ +
-20. Implement ''​rotate/​3''​ which rotates a list by a given number of positions+
-<​code>​ +
-?- rotate([1,2,3,4,​5,​6,​7,​8],​3,​R). +
-R = [4,5,6,​7,​8,​1,​2,​3]+
 </​code>​ </​code>​