点积 Prolog/3 需要 SUM 的提示 [英] Dot product Prolog/3 need Hint for the SUM

查看:11
本文介绍了点积 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天全站免登陆