Prolog代写 | COMP3411/9814 Artificial Intelligence

COMP3411/9814 Artificial Intelligence
Assignment 1 – Prolog and Search

Part 1 – Prolog
Question 1.1: List Processing
Write a predicate sumsq_even(Numbers, Sum) that sums the squares of only the
even numbers in a list of integers.
?- sumsq_even([1,3,5,2,-4,6,8,-7], Sum).
Sum = 120
Note that it is the element of the list, not its position, that should be tested for oddness.
(The example computes 2*2 + (-4)*(-4) + 6*6 + 8*8). Think carefully about how the
predicate should behave on the empty list — should it fail or is there a reasonable value
that Sum can be bound to?
To decide whether a number is even or odd, you can use the built-in Prolog operator N
mod M, which computes the remainder after dividing the whole number N by the whole
number M. Thus a number N is even if the goal 0 is N mod 2 succeeds. Remember that
arithmetic expressions like X + 1 and N mod M are only evaluated, in Prolog, if they
appear after the is operator. So 0 is N mod 2 works, but N mod 2 is 0 doesn’t work.
Question 1.2: List Processing
Eliza was the name of the first “chatbot” written by Joseph Weizenbaum at MIT in the
mid-1960s. It pretended to be a psychiatrist, so that it only had to do simple
transformations on the input and turn a statement into a sentence. If a sentence is
represented by a list of words, an example of a simple transformation is:
?- eliza1([you,do,not,like,me], X).
X = [what,makes,you,say,i,do,not,like,you]
Here, the simple transformation is to put “What makes you say” in the front of the
sentence and replace “you” with “i” and “me” with “you”.
Write a Prolog program that takes a sentence in the form of a list and replaces any
you → i
me → you
my → your
and prepends the list [what, makes, you, say] to the transformed list.
Note 1: your predicate MUST be called “eliza1”. Don’t forget the “1”.
Note 2: To prevent trying to print lists that are accidentally too long, SWI Prolog
limits the number of elements in a list that it prints. You might see the answer to your
query ending with [a, b c | …]. You can force SWI Prolog to print longer lists with the
:- set_prolog_flag(answer_write_options,[max_depth(0)]).
which you can put at the top of your file. max_depth(0) means no limit.
Question 1.3: List Processing
The rules in Question 1.2 work if “you” starts a sentence but won’t make much sense
for an example like this:
?- eliza1([i,wonder,if,you,would,help,me,learn,prolog], X).
X = [what,makes,you,say,i,if,wonder,i,would,help,you,learn,prolog]
What would be better is:
?- eliza2([i,wonder,if,you,would,help,me,learn,prolog], X).
X = [what,makes,you,think,i,would,help,you]
Write a predicate eliza2 (don’t forget the “2”) that takes a list of words:
[ …, you, , me, …]
and creates a new list of the form:
[what, makes, you, think, i, , you]
i.e. skip the words before “you” and after “me”, and insert the words in between “you”
and “me” into the new sentence between “i” and “you”.
Hint: You can use the built-in predicate “append(X, Y, Z)” to do a lot of the work for
you. Remember, “append” can be used to split a list, as well as concatenating lists.
Question 1.4: Prolog Terms
Arithmetic expressions can be written in prefix format, e.g 1+2*3 can be written as
add(1, mul(2, 3)). If the operators available are add, sub, mul, div, write a
Prolog program, eval(Expr, Val), that will evaluate an expression, e.g.
?- eval(add(1, mul(2, 3)), V).
V = 7
?- eval(div(add(1, mul(2, 3)), 2), V).
V = 3.5