UNIVERSITÄT BASEL Thorsten Möller Florian Spiess - [email protected] Programming Paradigms – Prolog Exercise 5 FS 2017 Due: 28.05.2017 23:59:59 Upload answers to the questions and source code before the deadline via courses.cs.unibas.ch. Since this is the last exercise, running programs do not have to be demonstrated during the exercise slots; only uploaded answers and source will be graded. Also note, of all mandatory exercises given throughout the course, you must score at least 2/3 of their total sum of points to get accepted in the final exam. Modalities of work: The exercise can be completed in groups of at the most 2 people. Do not forget to provide the full name of all group members together with the submitted solution. IMPORTANT: Because you will not have to present your answers to this exercise during the exercise lesson, you will have no chance to explain your answers. Please make sure that all your answers contain detailed explanations and are thoroughly documented. If your answer is ambiguous, unclear or your code is insufficiently documented, you will not be awarded the full score. Additionally, don’t forget to include the names of all team members in your answer, otherwise your answer might be corrected twice and, as a result, be falsely identified as plagiarism. Question 1: Bite-sized Prolog Tasks (5 points) For each of the following task descriptions write Prolog facts and/or rules that complete the task. Also include an example usage of the predicate that completes the task in a separate text file or as a comment in the code. a) Check if a list is sorted, assuming that it contains numerical values only. Example: ?- sorted([2,3,1]). false. (1 points) 1 b) Check if a three digit number is divisible without remainder, by another three digit number. (Your predicate should also be able to suggest possible divisors.) Example: ?- isDivisible(999,X). X = 1 ; X = 3 ; X = 9 ; X = 27 ; X = 37 ; X = 111 ; X = 333 ; X = 999. (2 points) c) Check if a number is a prime. Example: ?- isPrime(13). true. (2 points) Question 2: Knowledge Base (5 points) The following is a list of character occurrences in the first act of Shakespeare’s play Macbeth: 2 Act 1, Scene 1: - First Witch - Second Witch - Third Witch Act 1, Scene 2: - Duncan - Malcolm - Donalbain - Lennox - Ross - Sergeant - Attendants Act 1, Scene 3: - First Witch - Second Witch - Third Witch - Macbeth - Banquo - Ross - Angus Act 1, Scene 4: - Duncan - Malcolm - Donalbain - Lennox - Macbeth - Banquo - Ross - Angus - Attendants Act 1, Scene 5: - Lady Macbeth - Messenger - Macbeth Act 1, Scene 6: - Duncan - Malcolm - Donalbain - Banquo - Lennox - Macduff - Ross - Angus - Attendants - Lady Macbeth Act 1, Scene 7: - Macbeth - Lady Macbeth a) Write a Prolog knowledge base containing the above information. It should be possible to check if a character is in a given act and scene. The act should be part of the predicate to allow extension of the knowledge base for further acts. It should be possible to query the knowledge base to find out, for example, in what scenes a given character is present. (4 points) b) Explain the concept of backtracking in Prolog, including how it is responsible for being able to find a complete list of scenes in which a character is present in your implementation. (1 points) Question 3: 4x4 Sudoku (8 points) 4x4 Sudoku is a simplified version of sudoku (https://en.wikipedia.org/wiki/Sudoku), using only four 2x2 blocks of numbers instead of nine 3x3 blocks. In this task you will write a Prolog program that can solve such simple Sudokus. a) Write a predicate piece/1 that checks whether a variable is a valid number for a 4x4 Sudoku (1-4). (1 points) 3 b) Write a predicate valid/1 that checks that a list contains no duplicate values. (1 points) c) Use your piece and valid predicates and write a predicate that can find a solution to a partially solved 4x4 sudoku and determine if a given solution is valid. Example: ?- simple([_,1,3,_,2,_,_,_,_,_,_,3,_,2,1,_],Row1,Row2,Row3,Row4). Row1 = [4, 1, 3, 2], Row2 = [2, 3, 4, 1], Row3 = [1, 4, 2, 3], Row4 = [3, 2, 1, 4] . (6 points) Question 4: More Steganography (5 points) The following (rather strange) paragraph about the French novelist Balzac contains a hidden quote from the english translation of Dai Sijie’s novel ”Balzac et la Petit Tailleuse chinoise”. In this task you will write a Prolog program to extract this quote with the included array of indices. ”Balzac was a French novelist and playwright. His work inspired many fellow writers and is referenced in Dai Sijie’s ’Balzac and the Little Chinese Seamstress’, in which an uneducated mountain girl is transformed, and touched by the power of literature. He did not include an invisible wizard. This work proves the little seamstress had at least one finger, and a head with which she carried readers away to this dream.” [47, 11, 0, 14, 2, 46, 40, 34, 21, 60, 38, 68, 29, 30, 61, 27, 45, 57, 5, 63, 1, 32, 64, 66, 16, 2, 69] Write a predicate decipher/3 where the first argument is a string, the second a list of indices and the third is the resultant string such that the indices are replaced with the corresponding words from the string. Note that counting starts with zero, not one. Example: ?- decipher("I think apples are very much like pears.", [0,6,2], Result). Result = ’I like apples’ . Check your implementation with the given paragraph and index array. The result should be a meaningful string. 4
© Copyright 2026 Paperzz