为什么这不能在F#中编译

时间:2023-01-19 17:03:00

This compiles and works:

这编译和工作:

let rec HelloEternalWorld _ = 
  Console.ReadLine() |> printf "Input: %s\n"
  HelloEternalWorld 0

HelloEternalWorld 0

This does not compile:

这不编译:

let rec HelloEternalWorld = 
  Console.ReadLine() |> printf "%s\n"
  HelloEternalWorld

HelloEternalWorld

I try to understand why not?

我试着理解为什么不呢?

2 个解决方案

#1


All you're missing are parentheses, as it would compile if it were:

所有你缺少的是括号,因为如果它是:


let rec HelloEternalWorld() = 
  Console.ReadLine() |> printf "%s\n"
  HelloEternalWorld()


To define a function with no arguments you need the parentheses to distinguish the function from a simple value.

要定义不带参数的函数,需要使用括号将函数与简单值区分开来。

#2


Please post the error messages you get, they say everything you need!

请发布您收到的错误消息,他们会说出您需要的一切!

The value ... will be evaluated as part of its own definition.

值...将作为其自己定义的一部分进行评估。

Your code doesn't compile because you're declaring a recursive value (which doesn't exist) instead of a recursive function.

您的代码无法编译,因为您声明了一个递归值(不存在)而不是递归函数。

In order to make this a function, you'll have to write something like

为了使它成为一个函数,你必须写出类似的东西

let rec HelloEternalWorld() = 
  Console.ReadLine() |> printfn "%s"
  HelloEternalWorld()

which is now a function of type unit -> unit.

现在它是类型单位 - >单位的函数。

#1


All you're missing are parentheses, as it would compile if it were:

所有你缺少的是括号,因为如果它是:


let rec HelloEternalWorld() = 
  Console.ReadLine() |> printf "%s\n"
  HelloEternalWorld()


To define a function with no arguments you need the parentheses to distinguish the function from a simple value.

要定义不带参数的函数,需要使用括号将函数与简单值区分开来。

#2


Please post the error messages you get, they say everything you need!

请发布您收到的错误消息,他们会说出您需要的一切!

The value ... will be evaluated as part of its own definition.

值...将作为其自己定义的一部分进行评估。

Your code doesn't compile because you're declaring a recursive value (which doesn't exist) instead of a recursive function.

您的代码无法编译,因为您声明了一个递归值(不存在)而不是递归函数。

In order to make this a function, you'll have to write something like

为了使它成为一个函数,你必须写出类似的东西

let rec HelloEternalWorld() = 
  Console.ReadLine() |> printfn "%s"
  HelloEternalWorld()

which is now a function of type unit -> unit.

现在它是类型单位 - >单位的函数。