Assignment 5: Prolog¶
Answer each of the following questions using SWI-Prolog on Linux. Write all the code yourself and, unless a question says otherwise, you can use any standard functions available when you launch SWI-Prolog (don’t import any extra libraries). Write any helper functions you think are useful.
For these questions we are only concerned with the first solution that’s
returned. So you do not need to worry about extra solutions, or using the cut
operator !.
Also, you can assume that all obvious pre-conditions for a function are true, and so you don’t need to check if function inputs are valid.
When it is time to submit your work, please put all your functions into a
single file named a5.pl and submit it on Canvas.
(2 marks) Implement
makelist(N, X, Lst)that works as follows:?- makelist(3, a, X). X = [a, a, a] ?- makelist(4, [a,b], X). X = [[a, b], [a, b], [a, b], [a, b]]
In other words,
makelist(N, X, Lst)binds toLsta new list consisting ofNcopies ofX.You can assume
Nis 0 or greater.(2 marks) Implement
second_min(Lst, M)that calculates the second smallest number on a list like this:?- second_min([2,8,4,6], X). X = 4 ?- second_min([1,2], X). X = 2
If the passed-in list has fewer than 2 elements, it should fail:
?- second_min([], X). false ?- second_min([6], X). false
For simplicity, you can assume
Lsthas no duplicates.(2 marks) Prolog has a function called
numlist(Lo, Hi, Result)that creates a list of numbers fromLotoHi. For example:?- numlist(1,5,L). L = [1, 2, 3, 4, 5]
Implement your own version of this called
mynumlist(Lo, Hi, Result). Of course, don’t usenumlistanywhere!Here’s some documentation for numlist, and other useful list functions.
(2 marks) Implement the function
all_diff(Lst)that succeeds (i.e. returnstrue) just whenLsthas no duplicate values, e.g.:?- all_diff([7,2,1,9]). true ?- all_diff([7,2,7,9]). false
If
Lstis empty, or only has one element, thenall_diffshould succeed.You can use
\+, the not operator, in your solution. It works like this:?- \+ member(2, [2,4,1]). false ?- \+ 5 < 6. false.
(2 marks) Implement
negpos(L, Neg, NonNeg)that partitions a listLof numbers into negatives and non-negatives. For example:?- negpos([1,0,2,-3,2,-4,5], A, B). A = [-3, -4], B = [1, 0, 2, 2, 5]
The order of the numbers in
NegandNonNegdoesn’t matter.(5 marks) A 3x3 magic square is a grid of 9 numbers where each row and column add up to the same number (known as the magic number). The sum of the two diagonals does not matter.
For example, this magic square has magic number 15:
1 5 9 6 7 2 8 3 4
Implement
magic(L9, Result)that takes a listL9of 9 numbers as input, and calculates a permutation ofL9that is magic. For example:?- magic([1,2,3,4,5,6,7,8,9], Result). Result = [1, 5, 9, 6, 7, 2, 8, 3, 4]
Resultis in row-major order, i.e. it corresponds to this square:1 5 9 6 7 2 8 3 4
Here’s another example:
?- magic([2,4,6,8,10,12,14,16,18], Result). Result = [2, 10, 18, 12, 14, 4, 16, 6, 8]
This is the square (it’s magic number is 30):
2 10 18 12 14 4 16 6 8
If
L9does not have exactly 9 elements, thenmagicshould returnfalse.Depending upon the numbers in
L9, there could be 0 or more solutions. When there’s no solution, yourmagicfunction should only take a few seconds to run.