List Product Define a predicate list_prod/2 that takes a list of numbers as a fi
ID: 3598622 • Letter: L
Question
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. Examples: ?- 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.
Please answer in Prolog
Explanation / Answer
list_prod([], 0).
list_prod([X|Y], P) :- product(Y, X, P).
product([], P, P).
product([X|Y], X0, P) :- product(Y, X, P0), P is P0 * X0.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.