Programming Project #2 Prolog Programming Assignment Due: Apr 11, 2018 11:59pm
Define and test the Prolog predicates described below. Each of your predicates must have the same name and signature as the examples below. Your predicates must behave properly on all instances of valid input types.
All predicates should have only one definition for a given number of parameters. Your submission should consist of a single source code text file that includes all facts,
predicate definitions, and propositions.
Your file should be named <your_net_id>.prolog.
You may find additional Prolog language help at the following links:
- SWI-Prolog manual
- SWI-Prolog documentation
- Learn Prolog Now!
CS-4337 – Organization of Programming Languages
Page 1 of 11
1) Odd Multiple of 3 [10 points]
Define a predicate oddMultOf3/1 that determines whether an integer is an odd multiple of 3. A user should be able to enter the predicate with an integer, e.g. oddMultOf3(42) and evaluate to either true or false. If the given parameter is not an integer, your predicate should display the message “ERROR: The given parameter is not an integer”.
Examples:
CS-4337 – Organization of Programming Languages
?- oddMultOf3(171). true.
?- oddMultOf3(100). false.
?- oddMultOf3(12). false.
?- oddMultOf3(4.2). ERROR: The given parameter is not an integer
?- oddMultOf3(-9). true.
Page 2 of 11
2) List Product
Define a predicate list_prod/2 that takes a list of numbers as a first parameter and determines the product of all of the list elements in the second parameter. Your predicate should have the signature list_prod(+List, +Number). The product of an empty list should be zero. The product of an empty list is the special case where the value is 0.
Examples:
CS-4337 – Organization of Programming Languages
?- list_prod([4,3], Product). Product = 12.
?- list_prod([7,8,0,13], Product). Product = 0.
?- list_prod([6,2,5,10], Product). Product = 600.
?- list_prod([], Product). Product = 0.
Page 3 of 11
3) Palindrome
Define a predicate palindrome/1 that takes a list of numbers as a single parameter and evaluates whether the list is the same both backward and forward, i.e. a “palindrome” list. Your predicate should have the signature palindrome(List). Note that the list to be tested may be heterogenous data types.
Examples:
CS-4337 – Organization of Programming Languages
?- palindrome([4,3,4]). true.
?- palindrome([7,2,5,7]). false.
?- palindrome([d,4,4,d]). true.
?- palindrome([]). true.
?- palindrome([a]). true.
Page 4 of 11
4) Second Minimum [10 points]
Define a predicate secondMin/2 with the signature secondMin(List, Min2) where Min2 is the second lowest unique valued element in some list of numbers, List. If the list has fewer than two unique elements, then your predicate should display the following, “ERROR: List has fewer than two unique elements.” If one more elements of List is not a number, then your predicate should display the following for the first encounter of a non-number element, “ERROR: “element” is not a number.”, where element is the value of the non-number element.
Examples:
CS-4337 – Organization of Programming Languages
?- secondMin([17,29,11,62,37,53], M2). M2 = 17
?- secondMin([512], M2). ERROR: List has fewer than two unique elements.
?- secondMin([7,5.2,3,6,-3.6,9,-2], M2). M2 = -2
?- secondMin([12,2,b,7], M2). ERROR: "b" is not a number.
?- secondMin([3,3,3], M2). ERROR: List has fewer than two unique elements.
Page 5 of 11
5) Segregate
Define a predicate segregate/3 that takes a list and a predicate name as the first two parameters. The predicate name should be one that takes a single parameter and evaluates to true/false (e.g. even/1, or multipleOfSeven/1). The third parameter is a two element list with two sublists, the first sublist containing elements of the original list for which the second parameter predidcate is true, and the second sublist containing elements of the original list for which the second parameter predidcate is false. Your predicate should have the signature segregate(List, Pred, [X,Y]), where X and Y are lists. Note that you may not use the built-in filter/3 predicate, however you may design your own helper predicate(s).
Examples:
CS-4337 – Organization of Programming Languages
?- segregate([8,7,6,5,4,3], Even, Odd). Even = [8,6,4] Odd = [7,5,3]
?- segregate([7,2,3,5,8], Even, Odd). Even = [2,8] Odd = [7,3,5]
?- segregate([-4,11,-7,9,0], Even, Odd). Even = [-4,0] Odd = [11,-7,9]
?- segregate([5,13,29], Even, Odd). Even = [] Odd = [5,13,29]
?- segregate([], Even, Odd). Even = [] Odd = []
Page 6 of 11
6) Bookends
Design a predicate bookends/3 whose three parameters are all lists. the predicate tests if the first list parameter is a prefix of the third and if the second list parameter is a suffix of the third. Note that the lists in the first and second arguments may overlap. You may not use the built-in predicate reverse, however you may design your own helper predicate(s).
Examples:
CS-4337 – Organization of Programming Languages
?- bookends([1],[3,4,5],[1,2,3,4,5]). true.
?- bookends([],[4],[1,2,3,4]). true.
?- bookends([8,7,3],[3,4],[8,7,3,4]). true.
?- bookends([6],[9,3],[6,9,3,7]). false.
?- bookends([],[],[2,4,6]). true.
?- bookends([23],[23],[23]). true.
Page 7 of 11
7) Subslice
Design a predicate subslice/2 that tests if the first list argument is a contiguous series of elements anywhere within in the second list argument.
Examples:
CS-4337 – Organization of Programming Languages
?- subslice([2,3,4],[1,2,3,4]). true.
?- subslice([8,13],[3,4,8,13,7]). true.
?- subslice([3],[1,2,4]). false.
?- subslice([],[1,2,4]). true.
?- subslice([1,2,4],[]). false.
Page 8 of 11
8) Shift
Design a predicate shift/3 that “shifts” or “rotates” a list N places to the left. N may be a negative number, i.e. rotate to the right. Your predicate should have the signature shift(+List, +Integer, +List). Note that the rotated list should be the same length as the original list. You may not use the built-in predicate reverse, however you may design your own helper predicate(s).
Examples:
CS-4337 – Organization of Programming Languages
?- shift([a,b,c,d,e,f,g,h],3,Shifted). Shifted = [d,e,f,g,h,a,b,c]
?- shift([1,2,3,4,5],1,Shifted). Shifted = [2,3,4,5,1]
?- shift([a,b,c,d,e,f,g,h],-2,Shifted). Shifted = [g,h,a,b,c,d,e,f]
Page 9 of 11
9) Luhn Algorithm
Design a predicate luhn/3 that is an implementation of the Luhn Algorithm and returns true if the parameter is an integer that passes the Luhn test and false otherwise.
Examples:
CS-4337 – Organization of Programming Languages
?- luhn(799273987104). true.
?- luhn(49927398717). false.
?- luhn(49927398716). true.
Page 10 of 11
10) List of Parameters
Design a predicate (listOfParams/2) that aggregates all of of the parameters in a knowledge base for a given predicate that has a single parameter.
Examples:
CS-4337 – Organization of Programming Languages
% Knowledge Base male(adam). male(bob). male(brett). male(charles). male(chris). male(clay). female(ann). female(barbara). female(coraline).
?- listOfParams(male, L). L = [adam, bob, brett, charles, chris, clay].
?- listOfParams(female, [ann, barbara, coraline]). true.
Page 11 of 11