0

I need to add elements in an empty list in prolog but I am getting into an infinite loop. This is my code:

dot_product([],[],0).

dot_product([M|Ms],[N|Ns],Sum) :-
    dot_product(Ms,Ns,S),
    Sum is S+M*N.

add_tail([],X,[X]).

add_tail([H|T],X,[H|L]):-
    add_tail(T,X,L).

row([],[],[]).
row(RI,[],[]).
row(RI,[Y|O],R) :-
    dot_product(RI,Y,R1),
    add_tail([],R1,R),
    row(RI,O,R).  

Example input:

?-row([1,2],[[1,2],[3,4]],R). 

Expected output:

R=[5,11] 

but instead it goes into an infinite loop.

Nick
  • 138,499
  • 22
  • 57
  • 95
A.G
  • 41
  • 4
  • Possible duplicate of [Dot product Prolog/3 need Hint for the SUM](https://stackoverflow.com/questions/20619516/dot-product-prolog-3-need-hint-for-the-sum) – Guy Coder Nov 30 '18 at 19:00
  • Of interest: [Rosetta Code](http://rosettacode.org/wiki/Rosetta_Code) [Prolog](http://rosettacode.org/wiki/Category:Prolog) [Dot Product](https://rosettacode.org/wiki/Dot_product#Prolog) – Guy Coder Nov 30 '18 at 19:02
  • When I run you query using the posted code I get false. – Guy Coder Nov 30 '18 at 19:06
  • red(RI,[Y|O],R):-dot_product(RI,Y,R1),add_tail(R,R1,R),red(RI,O,R). this row sholud stay in the code – A.G Nov 30 '18 at 19:13
  • I have problem with this segment add_tail(R,R1,R) – A.G Nov 30 '18 at 19:13
  • If this code is to calculate a dot product in Prolog can you explain how your code is suppose to work. I have included links to two other versions in the comments and those versions are easy to understand. Your version has me confused. It seems to be doing more than is needed. – Guy Coder Nov 30 '18 at 19:17
  • I need to find a product of matrixwith it transpose so with this code I’m trying to find row od the product matrix – A.G Nov 30 '18 at 19:19
  • `I need to find a product of matrixwith it transpose so with this code I’m trying to find row od the product matrix` That has me even more confused. Based on that I would expect to see a transpose predicate but don't. Pretend I don't know how to calculate what you want and add any necessary comments to the code in your question. Seriously. I sometimes spend more than an hour writing out my question before posting and try hared to help them help me. You are making me do too much of the work by having to ask too many questions. – Guy Coder Nov 30 '18 at 19:24
  • Sorry this is my second queston here – A.G Nov 30 '18 at 19:26
  • Yes I know. Take a look at my [questions](https://stackoverflow.com/users/1243762/guy-coder?tab=questions). I am not saying I write the best questions, but I do get answers and accept most of them. When I don't accept an answer it is noted why. Notice how much detail is put into them. – Guy Coder Nov 30 '18 at 19:29

0 Answers0