Given the following Prolog function:
fn([Y], Y).
fn([_|Z], Y) :- fn(Z, Y).
(a) Determine what output will be produced if we run the query: fn([1, 2, 3], Y).
(b) Explain what the function fn does (do NOT list steps, give the final result).
PROLOG functions are quite straight forward such as :
addition(A, B, C):- C is A +
B.
subtraction(A, B, C):- C is A
- B.
division(A, B, C):- C is A /
B.
multiplication(A, B, C):- C
is A * B.
For the question you mentioned:
a) It will store the sum of all the three numbers into the Y variable and will give us the result
b) The function just takes the initial parameters and
perform operations on them and finally store the result into the
terminal variable.
HOpe this will help you.. THanks
Get Answers For Free
Most questions answered within 1 hours.