[英]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#interactive时,首先需要评估定义要使用的函数的代码。 因此,如果您只选择脚本的最后一行,则会出现错误。 我想您使用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
要修复问题,您需要选择文件中的所有内容(最后一行除外),然后按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处理了函数定义fact
, pow
和func1
,你现在可以在你正在评估的其他表达式中使用它们:
> let quarter = func1 1.0e-2 1.0 2;;
val quarter : float = 0.2250279793
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.