点产品Prolog/3要求SUM提示 [英] Dot product Prolog/3 need Hint for the SUM

查看:62
本文介绍了点产品Prolog/3要求SUM提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,我在序言中进行算术问题,是的,它是我搜索过的点积,发现一堆乱码与本书所要问的内容并不相等.它是/3,所以这是我到目前为止的结果,但我需要对两个列表的乘积结果求和.关于应该建议做什么的任何提示?

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).

推荐答案

您的问题是,您两次使用Mul表示您打算一次使用它,而Reuslt2在任何地方都不存在.可能您的意思是:

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.

这篇关于点产品Prolog/3要求SUM提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆