Write the following predicates in SWI-Prolog. 1, size. Given two parameters, the
ID: 3710822 • Letter: W
Question
Write the following predicates in SWI-Prolog.
1, size. Given two parameters, the first one being a list of numbers, returns the number of elements in the list in the second parameter.
size ([3, 5, 7, 8], X).
X = 4
size ([3], X).
x = 1.
2.
total. Given two parameters, the first one being a list of numbers, returns the sum of all elements in the list in the second parameter.
total ([3, 5, 7, 8], X).
X = 23
total ([3], X).
X = 3
3.
min. Given two parameters, the first one being a list of numbers, returns the minimum value in the list in the second parameter.
min ([3, 3, 8, 9, 2, 9, 2], X).
X = 2
min ([7], X).
X = 7
Explanation / Answer
ANSER
GIVEN BY
Q1
The prolog code would be
size([],0).
size([_|T],X):-size(T,N),X is N+1.
The output is
size([2,4,6,8],X).
X=4.
Q2
total([],0).
total([H|T],X):-total(T,N),X is N+H.
The output is
total([2,4,6,8],X).?
X = 20.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.