RebelsMascot
Programmer
Hey, first off I'm a complete newbie when it comes to Prolog.
My problem is that I'm trying to multiple two lists and sum the values, e.g.
List 1: [1,2,3]
List 2: [4,5,6]
and sum
Sum would be:
Sum = 1*4 + 2*5 + 3*6 = 32
Here's what I have so far:
My problem is the sum part, how do I initialise it to zero?
The checkList predicate is used to check if a list is empty and will return 0 for the ith element
listprod(X, Y, Sum):-
checkList(X, NewListX, Num1),
checkList(Y, NewListY, Num2),
Sum1 is Sum+(Num1*Num2),
listprod(NewListX, NewListY, Sum).
checkList(List, NewList, Int):-
List == [],
Int is 0,
NewList = List.
checkList(List, NewList, Int):-
not List == [],
[Head|Tail] = List,
Int is Head,
NewList = Tail.
Thanks for any help.
My problem is that I'm trying to multiple two lists and sum the values, e.g.
List 1: [1,2,3]
List 2: [4,5,6]
and sum
Sum would be:
Sum = 1*4 + 2*5 + 3*6 = 32
Here's what I have so far:
My problem is the sum part, how do I initialise it to zero?
The checkList predicate is used to check if a list is empty and will return 0 for the ith element
listprod(X, Y, Sum):-
checkList(X, NewListX, Num1),
checkList(Y, NewListY, Num2),
Sum1 is Sum+(Num1*Num2),
listprod(NewListX, NewListY, Sum).
checkList(List, NewList, Int):-
List == [],
Int is 0,
NewList = List.
checkList(List, NewList, Int):-
not List == [],
[Head|Tail] = List,
Int is Head,
NewList = Tail.
Thanks for any help.