0
votes

Give recursive definition in Prolog :

Define a predicate prod(L, N) which takes a list L of numbers and calculates the product N of the numbers in the list.

I have found a way that works for the sum :

  sum([],0).

 sum([X|L],N) :- sum(L,N1),N is N1 + X.

Please help me. I have been trying for a long time and it still isn't working.

1
you found a way or you found a code? - Will Ness

1 Answers

1
votes
product(L, N) :- product(L, 1, N).
product([], X, N) :- N is X.
product([H|T], X, N) :- X1 is X*H, product(T, X1, N).

You need a temporary counter (X is this case). And you mistook sum for multiplication.