Sml 程序 -> "AS syntax error" 上的混淆

Sml program -> confusion on "AS syntax error"

所以我必须用 SML 编写一个小程序 ->>

a file named ‘p0.sml’ that contains a function named epoly, which accepts as parameters a list of real values a0 through an, and a single real value x. The list contains the coefficients of a polynomial of the form a0 + a1x + a2x 2 + … + anx n, where the real x used is the x parameter passed to your function. Your implementation must accept the list of coefficients as the first parameter and the value of x as the second. Your function must return the value of the polynomial specified by the parameters passed to it.

这是我目前所拥有的,但由于 as 的语法错误而无法编译。 “错误:在 AS 发现语法错误”。如果您有任何指点,将不胜感激。

fun epoly([], x:real) = 0.0
= epoly(L:real list as h::T, x:real) = h + (x * epoly(T, x));

你好像打错字了。你的第二个 = 应该是 |.

fun epoly([], x:real) = 0.0
  | epoly(L:real list as h::T, x:real) = 
      h + (x * epoly(T, x));

此外,无需指定类型。您的 SML 编译器可以从提供的数据中推断出类型。随着删除不必要的绑定,这可以减少到:

fun epoly([], _) = 0.0
  | epoly(h::T, x) = 
      h + (x * epoly(T, x));

fun epoly([], _) = 0.0 我们知道 epoly 将采用列表和某种类型的元组和 return real.

发件人:

  | epoly(h::T, x) = 
      h + (x * epoly(T, x));

我们知道x乘以一个real,所以x一定是real。由于 h 被添加到 real,它必须是 real,所以整个列表是 real list.

因此epoly的类型可以正确推断为real list * real -> real.