【发布时间】:2014-09-25 19:12:53
【问题描述】:
我正在尝试在 SML 中实现 Horner's algorithm。
fun horner(lst1:real list,x:real) =
let
val i = ref 1
val result = ref (List.last(lst1))
in
if (lst1) = ([]:real list) then 0.0
else
while (!i <= length(lst1)-1) do
(result:=!result*x+List.nth(lst1,length(lst1)-(!i)-1);
i := !i+1);
!result
end;
将 x^n 的系数 a{n} 作为初始结果,然后使用 horner 计算多项式。
计算为 ((a{n}*x+a{n-1})*x+a{n-2})..列表包含多项式的系数。 问题是“if lst1 = []....else”部分。 仅使用 while 循环可使程序运行良好。 但我想不出这部分有什么问题。
【问题讨论】:
-
你必须告诉我们问题出在哪里……代码应该做什么?它实际上是做什么的?
-
你的代码目标是什么?
-
我编辑了细节。
-
每个输入的作用是什么?