简体   繁体   English

F#“​​值或构造函数......未定义”

[英]F# “The value or constructor … not defined”

So, this is my second F# console application. 所以,这是我的第二个F#控制台应用程序。 The code goes like this: 代码如下:

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;;  

When I try to run let quarter = func1 1.0e-2 1.0 2;; 当我尝试运行时, let quarter = func1 1.0e-2 1.0 2;; , interactive console says error FS0039: The value or constructor 'func1' is not defined . ,交互式控制台说error FS0039: The value or constructor 'func1' is not defined What did go wrong or what does my code lack? 出了什么问题或我的代码缺少什么? Thanks in advance. 提前致谢。

When you use F# interactive, you first need to evaluate the code that defines the functions that you want to use. 使用F#interactive时,首先需要评估定义要使用的函数的代码。 So, if you select just the last line of your script, you get an error. 因此,如果您只选择脚本的最后一行,则会出现错误。 I suppose you are using Alt+Enter to send the code to F# interactive - if you were copying the code and entering it by hand, you would see something like this: 我想您使用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

To fix things, you need to select everything in your file (except for the last line) and hit Alt+Enter. 要修复问题,您需要选择文件中的所有内容(最后一行除外),然后按Alt + Enter键。 Then you should see something like: 那你应该看到类似的东西:

>     
val fact : n:int -> float

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

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

This tells you that the F# Interactive processed the function definitions fact , pow and func1 and you can now use them in other expressions that you're evaluating: 这告诉你F#Interactive处理了函数定义factpowfunc1 ,你现在可以在你正在评估的其他表达式中使用它们:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM