F#"The value or constructor ... not defined"

F# "The value or constructor ... not defined"

所以,这是我的第二个 F# 控制台应用程序。 代码如下:

let rec fact n =


 if n = 1 then 1.0
  else float(n)*fact(n-1);;

let rec pow x n = 
  if n = 1 then x
  else float(x) * pow x (n-1);;

let rec func1 eps x n = 
    let  f = fact (2 * n) * (fun n -> pow x n / float(n)) (2*n+1) / (pow 4.0 n * pow (fact n) 2)
    if abs f < eps then f
    else f + func1 eps x (n+1);;

let  quarter = func1 1.0e-2 1.0 2;;  

当我尝试 运行 let quarter = func1 1.0e-2 1.0 2;; 时,交互式控制台显示 error FS0039: The value or constructor 'func1' is not defined。 哪里出了问题或者我的代码缺少什么? 提前致谢。

当您使用 F# 交互时,您首先需要评估定义您要使用的函数的代码。因此,如果您 select 只是脚本的最后一行,则会出现错误。我想您正在使用 Alt+Enter 将代码发送到 F# interactive - 如果您正在复制代码并手动输入它,您会看到类似这样的内容:

Microsoft (R) F# Interactive version 12.0.30815.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> let  quarter = func1 1.0e-2 1.0 2;;

  let  quarter = func1 1.0e-2 1.0 2;;
  ---------------^^^^^

stdin(1,16): error FS0039: The value or constructor 'func1' is not defined

要修复问题,您需要 select 文件中的所有内容(最后一行除外)并按 Alt+Enter。然后你应该看到类似的东西:

>     
val fact : n:int -> float

> 
val pow : x:float -> n:int -> float

> 
val func1 : eps:float -> x:float -> n:int -> float

这告诉您 F# Interactive 处理了函数定义 factpowfunc1,您现在可以在您正在计算的其他表达式中使用它们:

> let  quarter = func1 1.0e-2 1.0 2;;
val quarter : float = 0.2250279793