2

Good Day I am doing the problema of arithmetic in prolog and Yes its the dot Product I have Searched and found a mess of code that did not equal to what the book is asking me. Its a /3 so this is what i have so far but i need to sum the result of the product of both list. Any hint on what should be recommended to do?

    dot([HD|TL],[HD2|TL2],Result):-
       Mul is HD2 * HD,
       dot(TL,TL2,Mul),
       Result is Mul + Reuslt2.
    dot([],[],0).
false
  • 10,264
  • 13
  • 101
  • 209
HTLINK
  • 65
  • 1
  • 7
  • DEAR GOD THAT WAS IT..... I had days trying to figure it out.... so the function reserves that Variable did not thinked like that >. – HTLINK Dec 16 '13 at 20:04

2 Answers2

4

Your problems are that you're using Mul twice where you mean to use it once, and Reuslt2 doesn't exist anywhere. Probably what you mean is:

dot([], [], 0).
dot([H1|T1], [H2|T2], Result) :- 
  Prod is H1 * H2,
  dot(T1, T2, Remaining),
  Result is Prod + Remaining.
Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77
  • DEAR GOD THAT WAS IT..... I had days trying to figure it out.... so the function reserves that Variable did not thinked like that >. – HTLINK Dec 16 '13 at 20:05
2

You use SWI-Prolog, so you can try :

:- use_module(library(lambda)).
my_dot(L1,L2,R) :-
    foldl(\X^Y^Z^T^(T is X*Y + Z), L1,L2,0,R).
joel76
  • 5,565
  • 1
  • 18
  • 22
  • DEAR LORD JESUS CHRIST im still not in module xD getting there Little by Little but thanx for the advance tip ;) – HTLINK Dec 16 '13 at 22:47
  • lambda.pl can be found there : http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl – joel76 Dec 17 '13 at 10:59