From 9bf32e3aa583d44406e033669418a496cedb9181 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 18 Jan 2018 18:01:08 -0800 Subject: [PATCH] Finish ch05 practical. --- lpn/ch05/practical.pl | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lpn/ch05/practical.pl b/lpn/ch05/practical.pl index b9b42f3..e99173e 100644 --- a/lpn/ch05/practical.pl +++ b/lpn/ch05/practical.pl @@ -34,10 +34,9 @@ min([H|T], N) :- %% should yield %% Result = [6,21,12] scalarMult(_, [], []). -scalarMult(K, [H|T], R) :- - V is K * H, - scalarMult(K, T, [R|V]). - +scalarMult(K, [H|T], [V|R]) :- + V is K * H, + scalarMult(K, T, R). %% 3. Another fundamental operation on vectors is the dot product. This %% operation combines two vectors of the same dimension and yields a @@ -51,4 +50,9 @@ scalarMult(K, [H|T], R) :- %% the query %% ?- dot([2,5,6],[3,4,1],Result). %% should yield -%% Result = 32 \ No newline at end of file +%% Result = 32 +dot([], [], 0). +dot([X|TX], [Y|TY], R) :- + dot(TX, TY, R2), + V is X * Y, + R is R2 + V. \ No newline at end of file