lambda - Compute Dot Product of Two Vectors -
i'm supposed create predicate in prolog such iprod(list1, list2, result)
takes 2 lists of equal length , each contain integers. result dot product of 2 vectors.
for example, list1 = [1,2,3]
, list2 = [4,5,6]
, result 1*4 + 2*5 + 3*6
. i'm not supposed use built-in dotproduct function.
my code far:
iprod([],[], 0). iprod([h1|list1], [h2|list2], result h1 * h2) :- iprod(list1, list2, result).
in visual prolog:
domains ilist=integer* predicates iprod(ilist, ilist, integer, integer) clauses iprod([], _, r, r). iprod([x|xs], [y|ys], a, r):- m = x * y, rnew = + m, iprod(xs, ys, rnew, r). goal iprod([1,2,3],[4,5,6], 0, r).
results in 32
. sorry, no other prolog implementation available @ hand.
Comments
Post a Comment